JavaScript and client side scripting.
Moderator: General Moderators
desperado
Forum Commoner
Posts: 46 Joined: Wed Dec 10, 2008 8:49 am
Post
by desperado » Tue Oct 20, 2009 10:37 am
Anyone know how I can increment a variable name within a loop?
I have:
Code: Select all
var j=1;
for (j=1;j<=4;j++){
if (document.form1.elements['bh_hole_diam'+j].value > 0){
var Hole+j = Math.pow((document.form1.elements['bh_hole_diam'+j].value + 0.6),2);
}else{
var Hole+j = 0;
}
if ((document.form1.elements['bh_notch_w'+j].value > 0) && (document.form1.elements['bh_notch_h'+j].value > 0)){
var Notch+j = ((document.form1.elements['bh_notch_w'+j].value + 0.3) * (document.form1.elements['bh_notch_h'+j].value + 0.6));
}else{
var Notch+j = 0;
}
}
I'm trying to get results as:
Hole1=
Hole2=
Hole3=
...
kaszu
Forum Regular
Posts: 749 Joined: Wed Jul 19, 2006 7:29 am
Post
by kaszu » Tue Oct 20, 2009 11:36 am
Better use array
Code: Select all
var Hole = [];
for (j=1;j<=4;j++){
if (...) {
Hole[j] = ...;
} else {
Hole[j] = 0;
}
...
}
AbraCadaver
DevNet Master
Posts: 2572 Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:
Post
by AbraCadaver » Tue Oct 20, 2009 3:55 pm
I agree on using arrays, but to answer the question (not tested):
1. eval('hole' + j + ' = 0');
or
2. this["hole" + j] = 0;
mysql_function(): WARNING : This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.