Iterate through <select> options using jQuery

In this tutorial, let’s discuss that how can we iterate through the <select> options using jQuery?
Submitted by Pratishtha Saxena, on June 26, 2022

Whenever any iterations have to be made, generally forEach(), map(), and filter() methods are used. To traverse the <select> tag, a function has to be created which will return the text, value, or both (text and value) of the options in it.

For this, each() method of jQuery will be used. Using this, elements can be traversed easily. each() method allows us to declare a function that runs for every matched element that has been traversed.


$(selector).each(function(index, element)); 

The function can be created in such a way that it accepts index and element as its parameters as shown.

Now, to make sure that each() method iterates all the options of the select tag, we'll have to specify the id of the select tag and then mention the option after it.

Let's see an example for a better understanding,


HTML Code:

<!DOCTYPE html>

<html lang="en">
      <meta charset="UTF-8">
      <script src=""></script>
         <select id="mySelect">
            <option value="one">Laptop</option>
            <option value="two">Watch</option>
            <option value="three">Mobile</option>

jQuery Function:

    $("#mySelect option").each(function() {
        console.log('Text:-' + this.text + '  Value:-' + this.value);


Example: Iterate through select-options


Comments and Discussions!

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© some rights reserved.