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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Looping through form elements set as arrays

Status
Not open for further replies.

Sleidia

Technical User
May 4, 2001
1,284
FR
Hi :)

Let's say I have this form :

Code:
<input type="hidden" name="cat_ID[]">
<input type="hidden" name="cat_order[]">
<input type="hidden" name="cat_ID[]">
<input type="hidden" name="cat_order[]">
<input type="hidden" name="cat_ID[]">
<input type="hidden" name="cat_order[]">
<input type="hidden" name="cat_ID[]">
<input type="hidden" name="cat_order[]">

With javascript, how do I loop through cat_order[] elements and increment its value by 1 at each iteration?

document.getElementsByName( 'cat_order' ) doesn't seem to work :(

Thanks for the help!
 

Got it :

Code:
var elem_num=document.getElementsByName("cat_order[]");
var elem_tot = elem_num.length;
var test = '\n';

    for ( i=0 ; i<elem_tot ; i++ ) {

    elem_num[i].value = i + 1;
    
    test = test + elem_num[i].value + '\n';
    
    }

alert(test);

}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top