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!

loop through every select box based on a constant class attribute

Status
Not open for further replies.

ctenicki

Programmer
May 13, 2002
10
US
i came up with this code that will validate a select box based on a class attribute. however, i need to cycle or loop through all select boxes no matter how many are generated (based on the class attribute). the following code only validates the first select box. any help would be greatly appreciated.

function validateUser(form) {

if ((form.elements[0].className =="Test") && (form.elements[0].value =="Select an item"))
{
alert("You must select a reason");
return false;
}
else {return true;}
}
</script>


<TR><TD COLSPAN=3><select class=&quot;Test&quot; name=&quot;dropDown&quot;>
<OPTION value=&quot;Select an item&quot;>Select an item</OPTION>
<OPTION value=&quot;Acceptable&quot;>Acceptable</OPTION>
<OPTION value=&quot;Repair&quot;>Repair</OPTION>
<OPTION value=&quot;Replace&quot;>Replace</OPTION>
<OPTION value=&quot;Unacceptable&quot;>Unacceptable</OPTION>
</SELECT></TD></TR>

<TR><TD COLSPAN=3><select class=&quot;Test&quot; name=&quot;dropDown2&quot;>
<OPTION value=&quot;Select an item&quot;>Select an item</OPTION>
<OPTION value=&quot;Accept&quot;>Accept</OPTION>
<OPTION value=&quot;Replace&quot;>Replace</OPTION>
</SELECT></TD></TR>

<TR><TD COLSPAN=3><TABLE>
<INPUT type=&quot;submit&quot; value=&quot;Submit&quot; ONCLICK=&quot;return validateUser(document.forms[0])&quot; name=&quot;Submit&quot;>
 
put your code in a for loop

var ieleTotal = document.yourFormName.elements.length;

for (i=0;i<ieleTotal;i++)
{
if (document.yourFormName.elements.className ==&quot;Test&quot;) {
.... (your code)
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top