/    Sign up×
Community /Pin to ProfileBookmark

is looping through form elements possible?

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

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@cul8er23Sep 02.2003 — [i]Originally posted by kinetek [/i]

[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
×

Success!

Help @kinetek spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...