Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Mar 2006
    Location
    In an apartment.
    Posts
    57
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Doing calculations on in input that might not exist

    I have the following command (if that's the right term):

    Code:
    var taxTotal = getTaxTotals('tax', taxdescription);
    	document.getElementById(taxdescription + "-total").value = doFormat(taxTotal, 4);
    This is part of a much larger function, where taxdescription is passed as an argument. I'm not going to print up the whole larger function, nor the function getTaxTotals, as I don't think either is pertinent to the issue I'm facing.

    The problem is that the id (taxdescription + "-total") doesn't necessarily exist(it might, it might not, but the fields that would be summed to calculate it always do), so when the JavaScript tries to perform
    Code:
    document.getElementById(taxdescription + "-total").value = doFormat(taxTotal, 4);
    it throws up an error that
    Code:
    document.getElementById(taxdescription + "-total") has no properties
    This is ok, sort of, since all the other calculations are done properly and if that field doesn't exist we don't need to have it totalled up anyway.

    Basically what I'm asking is how do I re-write
    Code:
    document.getElementById(taxdescription + "-total").value = doFormat(taxTotal, 4);
    so that the JavaScript won't give the error when the element doesn't exist? For instance could you write something like
    Code:
    ifExists (getElementById(taxdescription + "-total")) {
    then enter
    Code:
    document.getElementById(taxdescription + "-total").value = doFormat(taxTotal, 4);
    }
    ?

  • #2
    New Coder
    Join Date
    Apr 2006
    Location
    Joussard, Alberta
    Posts
    89
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Preface with : I'm new to javascript but trying to help here as much as possible to pay forward the help I've received and to learn....

    would:

    Code:
    var amount=document.getElementById(taxdescription + "-total").value
    if (amount.length!=0){ amount=doFormat(taxTotal, 4) }
    work?

    Though from your snippit it looks to me like you will be overwriting whatever happened to be the value of taxdescription + "-total" with the return from doFormat() ?
    Last edited by nathan_lamothe; 04-27-2006 at 06:38 PM.

  • #3
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    Right, I think he wants to display the result of doFormat into the field -total. Your method would store the total string in a local variable.

    Instead, try:

    [code]
    var field = document.getElementById(taxdescription + "-total");
    if (field)
    field.value = doFormat(taxTotal, 4);

  • #4
    New Coder
    Join Date
    Apr 2006
    Location
    Joussard, Alberta
    Posts
    89
    Thanks
    0
    Thanked 0 Times in 0 Posts
    <aside>
    Hey Beagle,
    - if (field) tests field and if it anything other than null it is the same as if(true) right?
    - I was struggeling to interpret the getElementById(taxdescription + "-total") statement... is the actual item id

    '-total'

    or is the id

    the content of a variable taxdescription concatenated (+) with the value of something with the id '-total'

    or something else?

    </aside>

  • #5
    New Coder
    Join Date
    Mar 2006
    Location
    In an apartment.
    Posts
    57
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by nathan_lamothe
    <aside>
    Hey Beagle,
    - if (field) tests field and if it anything other than null it is the same as if(true) right?
    - I was struggeling to interpret the getElementById(taxdescription + "-total") statement... is the actual item id

    '-total'

    or is the id

    the content of a variable taxdescription concatenated (+) with the value of something with the id '-total'

    or something else?

    </aside>
    The latter- the id would be something like "Alberta tax-total" where 'Alberta tax' is the tax description.

    In other news,
    Code:
    var field = document.getElementById(taxdescription + "-total");
    if (field)
    field.value = doFormat(taxTotal, 4);
    is exactly right. Thanks for the help!

  • #6
    New Coder
    Join Date
    Apr 2006
    Location
    Joussard, Alberta
    Posts
    89
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Cool.

    Problem solved then?

  • #7
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    nathan:

    Yeah, in JavaScript, all non-0 values are true. Hence, false == 0. However, don't be fooled, false !== 0 because false and 0 are not of the same type (boolean and int respectively).

    null and undefined are also evaluated as 0 for purposes of conditional testing. So, if document.getElementById returns a real value (a pointer to an object) then it's non-0, hence true. But if it returns null, it's evaluated to 0 and hence false.

  • #8
    New Coder
    Join Date
    Apr 2006
    Location
    Joussard, Alberta
    Posts
    89
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks!

    #still_learning
    {display: visable;
    }



  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •