Click to See Complete Forum and Search --> : is looping through form elements possible?


kinetek
09-02-2003, 12:55 AM
is it possible to loop through form elements like how you would loop through forms on page? i'm thinking the code should look like this:

var x;
for (x = 0; x < 29; x++) {
var temp = (document.tea_lab.elements[x].options[document.tea_lab.elements[x].selectedIndex].value);
//then do something with temp
}

but it doesnt seem to be parsing the variable x. any help would be great. thanks

cul8er23
09-02-2003, 02:18 AM
[QUOTE]Originally posted by kinetek
[B]is it possible to loop through form elements like how you

This should work:


<html>
<head>
<script language="javascript">
function cycleSelect(){
for (var x = 0; x <2; x++) {
var temp = (document.tea_lab.elements[x].options[document.tea_lab.elements[x].selectedIndex].text);
//then do something with temp
alert(temp);
}
}
</script>
</head>

<body>
<form name="tea_lab" action="">
<select name="top2" size="1">
<option>Eminem</option>
<option>Michael Jackson</option>
</select>
<select name="top3" size="1">
<option>Tom Waits</option>
<option>Nina Hagen</option>
<option>Frank Zappa</option>
</select>
<input type=button name=button1 value="whatever" onClick="cycleSelect()"/>
</form>

</body>
</html>

Was that what you intended?

Cheers
Fab