...

View Full Version : Doing calculations on in input that might not exist



djmonkey1
04-27-2006, 05:47 PM
I have the following command (if that's the right term):


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
document.getElementById(taxdescription + "-total").value = doFormat(taxTotal, 4); it throws up an error that
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
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
ifExists (getElementById(taxdescription + "-total")) { then enter
document.getElementById(taxdescription + "-total").value = doFormat(taxTotal, 4);
}?

nathan_lamothe
04-27-2006, 07:15 PM
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:


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() ?

Beagle
04-27-2006, 07:33 PM
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);

nathan_lamothe
04-27-2006, 07:45 PM
<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>

djmonkey1
04-27-2006, 08:12 PM
<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,
var field = document.getElementById(taxdescription + "-total");
if (field)
field.value = doFormat(taxTotal, 4); is exactly right. Thanks for the help!

nathan_lamothe
04-27-2006, 08:48 PM
Cool.

Problem solved then?

Beagle
04-27-2006, 09:02 PM
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.

nathan_lamothe
04-27-2006, 09:18 PM
Thanks!

#still_learning
{display: visable;
}

:)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum