Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-27-2006, 04:47 PM   PM User | #1
djmonkey1
New Coder

 
Join Date: Mar 2006
Location: In an apartment.
Posts: 57
Thanks: 0
Thanked 0 Times in 0 Posts
djmonkey1 is an unknown quantity at this point
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);
}
?
djmonkey1 is offline   Reply With Quote
Old 04-27-2006, 06:15 PM   PM User | #2
nathan_lamothe
New Coder

 
Join Date: Apr 2006
Location: Joussard, Alberta
Posts: 89
Thanks: 0
Thanked 0 Times in 0 Posts
nathan_lamothe is an unknown quantity at this point
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..
nathan_lamothe is offline   Reply With Quote
Old 04-27-2006, 06:33 PM   PM User | #3
Beagle
Senior Coder

 
Join Date: Jul 2005
Location: New York, NY
Posts: 1,084
Thanks: 4
Thanked 19 Times in 19 Posts
Beagle is an unknown quantity at this point
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);
Beagle is offline   Reply With Quote
Old 04-27-2006, 06:45 PM   PM User | #4
nathan_lamothe
New Coder

 
Join Date: Apr 2006
Location: Joussard, Alberta
Posts: 89
Thanks: 0
Thanked 0 Times in 0 Posts
nathan_lamothe is an unknown quantity at this point
<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>
nathan_lamothe is offline   Reply With Quote
Old 04-27-2006, 07:12 PM   PM User | #5
djmonkey1
New Coder

 
Join Date: Mar 2006
Location: In an apartment.
Posts: 57
Thanks: 0
Thanked 0 Times in 0 Posts
djmonkey1 is an unknown quantity at this point
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!
djmonkey1 is offline   Reply With Quote
Old 04-27-2006, 07:48 PM   PM User | #6
nathan_lamothe
New Coder

 
Join Date: Apr 2006
Location: Joussard, Alberta
Posts: 89
Thanks: 0
Thanked 0 Times in 0 Posts
nathan_lamothe is an unknown quantity at this point
Cool.

Problem solved then?
nathan_lamothe is offline   Reply With Quote
Old 04-27-2006, 08:02 PM   PM User | #7
Beagle
Senior Coder

 
Join Date: Jul 2005
Location: New York, NY
Posts: 1,084
Thanks: 4
Thanked 19 Times in 19 Posts
Beagle is an unknown quantity at this point
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.
Beagle is offline   Reply With Quote
Old 04-27-2006, 08:18 PM   PM User | #8
nathan_lamothe
New Coder

 
Join Date: Apr 2006
Location: Joussard, Alberta
Posts: 89
Thanks: 0
Thanked 0 Times in 0 Posts
nathan_lamothe is an unknown quantity at this point
Thanks!

#still_learning
{display: visable;
}

nathan_lamothe is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:19 AM.


Advertisement
Log in to turn off these ads.