HighDots Forums  

Create dynamic variable names

JavaScript discussion (multi-lingual) JavaScript discussion (alt.comp.lang.javascript)


Discuss Create dynamic variable names in the JavaScript discussion (multi-lingual) forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
RolandD
 
Posts: n/a

Default Create dynamic variable names - 07-23-2005 , 10:39 AM






Hello all,

For a local invoice program I am writing, I want to calculate the
subtotal per line. Whenever a value is changed on that line the function
calculate_subtotal is called. On this line I have three variables:
product_aantal_ *line number*
product_prijs_ *line number*
product_totaal_ *line number*

*line number* can be any number between 0 and 9. It depends on the line
being changed.

The below code works fine in Opera and in IE but not in Firefox. I
managed to get it working in FF by using eval() but then it no longer
works in Opera or IE.

I have tracked it down to the problem being
getElementById('product_aantal_'+regel). It doesn't like the +line to
create the variable name.

Is there a uniform solution to this or is FF just not Javascript enough
compliant?

Thanks a lot,

RolandD

begin code:
----------------------
function calculate_subtotal(line) {
var regel = line.name.charAt(line.name.length - 1);
var aantal_waarde =
document.getElementById('product_aantal_'+regel).v alue;
var prijs_waarde = document.getElementById('product_prijs_'+regel).va lue;
var product_waarde =
document.getElementById('product_totaal_'+regel).v alue;
subtotaal = aantal_waarde*prijs_waarde;
subtotaal = formatAsMoney(subtotaal);
document.getElementById('product_totaal_'+regel).v alue = subtotaal;
}
----------------------
end code

Reply With Quote
  #2  
Old   
Kimmo Laine
 
Posts: n/a

Default Re: Create dynamic variable names - 07-25-2005 , 10:06 AM






"RolandD" <rdalmulderno (AT) spamhotmail (DOT) com> kirjoitti
viestissä:42e264bb$0$12749$ba620dc5 (AT) text (DOT) nova.planet.nl...
Quote:
Hello all,

For a local invoice program I am writing, I want to calculate the subtotal
per line. Whenever a value is changed on that line the function
calculate_subtotal is called. On this line I have three variables:
product_aantal_ *line number*
product_prijs_ *line number*
product_totaal_ *line number*

*line number* can be any number between 0 and 9. It depends on the line
being changed.

The below code works fine in Opera and in IE but not in Firefox. I managed
to get it working in FF by using eval() but then it no longer works in
Opera or IE.

I have tracked it down to the problem being
getElementById('product_aantal_'+regel). It doesn't like the +line to
create the variable name.

Is there a uniform solution to this or is FF just not Javascript enough
compliant?


This is just a guess, but it's the most common mistake people make. Let me
guess, in your page you have inputs that you call with getElementById()? And
the elemenets have names.
<input name="product_aantal_7" type="...>

Do they have id's? So that you can call an element by it's id, it must have
one. And the most common problem is that internet explorer treats names as
id's. Unless you have

<input name="product_aantal_7" id="product_aantal_7" type="...>

in your form, getElementById(...) will work in IE, but _not_ in Firefox. IE
is the one to blame, Fx works correcttly as it ignores the name attribute if
no ID is provided. name is not the same as id. Make sure you are using id's
in your form elements.

--
"I am pro death penalty. That way people learn
their lesson for the next time." -- Britney Spears

eternal.erectionN0 (AT) 5P4Mgmail (DOT) com




Reply With Quote
  #3  
Old   
RolandD
 
Posts: n/a

Default Re: Create dynamic variable names - 07-26-2005 , 04:24 PM



Hello Kimmo,

Thanks a lot, that was the issue. Spot on. Thanks a million. A real noob
issue if you ask me

Regards,

Roland

Reply With Quote
Reply




Thread Tools
Display Modes

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

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



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.