Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A loop question

Status
Not open for further replies.

phpPete

Programmer
Feb 25, 2002
88
US
OK, this one has me a bit bonkers. I have a dynamically generated form, using PHP. I want to use JS to move a value into a field upon selection.

Each row is 5 fields long, the number of rows vary depending upon the query, but the same five fields are gernerated each row.

The value I want to move is split(":") from the value (value="1.75:butter" )of a select box named selIngredients$i where $i is a counter varable concatenated to selIngredients.

On each iteration of a loop I want to take cost[0] and move it to document.frmAddAndCost.elements["tfItemCost"+ i].

I encounter a not an object error after several loops, which I assume is the counter going beyond the elements array boundary.

Any suggestions would be appreciated.

I know I can re-post the page and have PHP do this, but I'd prefer to use JS in this case.

Thanks in advance,

Pete

 
Post some more code!!

Also, a debugging note: After your page has rendered, view the html source. Make sure that the elements that you are creating dynamically are being named correctly. Be sure the name that is being generated in the html source matches the name being used in the javascript when you are attempting to set its value.
 
Here's what I'm working with so far.

note** Viewing source shows all is fine re: field names.



function shiftCost()// called by event clicking "doCost" button
{
var cost = new Array( frmAddAndCost.length - 3 );// declare new array of form length - 3
for(i = 0; i < (cost.length - 3); i++)
{
cost = document.frmAddAndCost.elements.value.substring(0,4);
document.frmAddAndCost.elements[i+4].value = cost;
alert( cost );// checking value of cost

}
}
 
I tried setting the for loop to:
for( i = 0; i < cost.length - 3; i+5)

but this causes an infinite loop
 
Apparently, javascript is returning a NaN when you are attempting to get the length of an array in which you have not added any elements.

try this out:

var len = frmAddAndCost.length - 3
var cost = new Array(len)

for (i=0< len; i+5)
{
...add your code here...
}

You may need to adjust the indexes on your loop. I am not sure how you have it constructed. Another hint: you may want to build a hidden field in you html whose value is the length of your rows. Its value would be created by an incremental variable in you html loop. You would create the hidden field after you have completed your loop. This would allow you to capture the value of your row count exactly in your javascript function w/o subtraction. You would not have to worry about decreasing the form.length by the extra elements you have in your form. Everytime you add/or subtract other elements to your form, you will throw off the number in your javascript function if you do not also account for this.

 
Solution ( for the moment ):

Oddly enough, I think my problem was my concatenation. Anyhow, here's what I have now and it's working for me, though I'll heed your advice about the hidden field, nice little &quot;trick&quot;

I appreciate the help.

Pete
function shiftCost()// called by event clicking &quot;doCost&quot; button
{

for(i = 0; i < ( (frmAddAndCost.length -3) / 5 ); i++)
{
cost = document.frmAddAndCost.elements[&quot;selIngredients&quot; + i].value.split(&quot;:&quot;);
document.frmAddAndCost.elements[&quot;tfItemCost&quot; + i].value = cost[0];
//alert( cost[i+2] );// checking value of cost

}
}
 
uhh... Of course
Code:
for (i=0< len; i+5)
{
  ...add your code here...
}
is infinite. The for() loop, as we all know, does not try to find the return value for its third part, but rather simply uses it for more script execution.

In other words, use the += operator.
Code:
for (i=0< len; i+=5)
{
  ...add your code here...
}
bluebrain.gif
blueuniment.gif
 
&quot;.....The for() loop, as we all know, does not try to find the return value for its third part, but rather simply uses it for more script execution.....&quot;

Missed that one.

Thanks for the reminder
 
Uhm... The third statement in the
Code:
for()
loop doesn't do anything unusual, such as assign a new value to your variable. Instead, it just provides space for statement execution.
bluebrain.gif
blueuniment.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top