Looping through the items in a select box using JS
Use the code given below to parse through the options in a select box and alert the Text and Value of each options. you can alter the function as per your need.
sample code is given below:
<form>
<select id="test" name="test">
<option value="First Value" >First Text</option>
<option value="Second Value" >Second Text</option>
<option value="Third Value" >Third Text</option>
<option value="Fourth Value" >Fourth Text</option>
<option value="Fifth Value" >Fifth Text</option>
</select>
<input type="button" value="Check" onclick="loopSelectBox('test')" >
</form>
<script>
function loopSelectBox(selectBoxId){
var obj=document.getElementById(selectBoxId);
for(i=0; i < obj.options.length ;i++){
alert("Value : " + obj.options[i].value + " --- Text : "+ obj.options[i].text );
}
}
</script>
Use the code given below to parse through the options in a select box and alert the Text and Value of each options. you can alter the function as per your need.
sample code is given below:
<form>
<select id="test" name="test">
<option value="First Value" >First Text</option>
<option value="Second Value" >Second Text</option>
<option value="Third Value" >Third Text</option>
<option value="Fourth Value" >Fourth Text</option>
<option value="Fifth Value" >Fifth Text</option>
</select>
<input type="button" value="Check" onclick="loopSelectBox('test')" >
</form>
<script>
function loopSelectBox(selectBoxId){
var obj=document.getElementById(selectBoxId);
for(i=0; i < obj.options.length ;i++){
alert("Value : " + obj.options[i].value + " --- Text : "+ obj.options[i].text );
}
}
</script>
Comments
Post a Comment