Home  »     »     »  How to loop through the items in a select box using Javascript?

How to loop through the items in a select box using Javascript?

Thursday, December 12, 2013

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>






How to link to this page?
If you wish to link to this page from your website, simply Copy and paste the above HTML code to your web page. It will appear on your page as:
How to loop through the items in a select box using Javascript?.




Share this!




comments powered by Disqus

This Weeks 7 Popular Posts


Subscribe to Recent Posts by Email
Stay connected to CROZOOM with regular Email notices of new Techie articles and IT Jobs. Updates will be delivered to your Inbox as soon as they are posted online.

Enter Your Email Address:  

Delivered by FeedBurner   RSS Feed

Search this Blog   



Urgent Openings for PHP trainees, Andriod / IOS developers and PHP developers in Kochi Trivandrum Calicut and Bangalore. Please Send Your updated resumes to recruit.vo@gmail.com   Read more »