Iterate through <select> options

Jquery

Jquery Problem Overview


I have a <select> element in HTML. This element represents a drop down list. I'm trying to understand how to iterate through the options in the <select> element via JQuery.

How do I use JQuery to display the value and text of each option in a <select> element? I just want to display them in an alert() box.

Jquery Solutions


Solution 1 - Jquery

$("#selectId > option").each(function() {
    alert(this.text + ' ' + this.value);
});

Solution 2 - Jquery

This worked for me

$(function() {
    $("#select option").each(function(i){
        alert($(this).text() + " : " + $(this).val());
    });
});

Solution 3 - Jquery

can also Use parameterized each with index and the element.

$('#selectIntegrationConf').find('option').each(function(index,element){
 console.log(index);
 console.log(element.value);
 console.log(element.text);
 });

// this will also work

$('#selectIntegrationConf option').each(function(index,element){
 console.log(index);
 console.log(element.value);
 console.log(element.text);
 });

Solution 4 - Jquery

And the requisite, non-jquery way, for followers, since google seems to send everyone here:

  var select = document.getElementById("select_id");
  for (var i = 0; i < select.length; i++){
    var option = select.options[i];
    // now have option.text, option.value
  }
 

Solution 5 - Jquery

If you don't want Jquery (and can use ES6)

for (const option of document.getElementById('mySelect')) {
  console.log(option);
}

Solution 6 - Jquery

You can try like this too.

Your HTML Code

<select id="mySelectionBox">
    <option value="hello">Foo</option>
    <option value="hello1">Foo1</option>
    <option value="hello2">Foo2</option>
    <option value="hello3">Foo3</option>
</select>

You JQuery Code

$("#mySelectionBox option").each(function() {
    alert(this.text + ' ' + this.value);
});

OR

var select =  $('#mySelectionBox')[0];

  for (var i = 0; i < select.length; i++){
    var option = select.options[i];
    alert (option.text + ' ' + option.value);
  }

Solution 7 - Jquery

$.each($("#MySelect option"), function(){
                    alert($(this).text() + " - " + $(this).val());                    
                });

Solution 8 - Jquery

Another variation on the already proposed answers without jQuery.

Object.values(document.getElementById('mySelect').options).forEach(option => alert(option))

Solution 9 - Jquery

After try several code, and still not working, I go to official documentation of select2.js. here the link: https://select2.org/programmatic-control/add-select-clear-items

from that the way to clear selection select2 js is:

$('#mySelect2').val(null).trigger('change');

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser208662View Question on Stackoverflow
Solution 1 - Jquerykarim79View Answer on Stackoverflow
Solution 2 - JqueryIT pplView Answer on Stackoverflow
Solution 3 - JqueryArun Pratap SinghView Answer on Stackoverflow
Solution 4 - JqueryrogerdpackView Answer on Stackoverflow
Solution 5 - JqueryJeyko CaicedoView Answer on Stackoverflow
Solution 6 - JqueryDulacosteView Answer on Stackoverflow
Solution 7 - JqueryYojana AmbavkarView Answer on Stackoverflow
Solution 8 - JqueryzzartView Answer on Stackoverflow
Solution 9 - Jqueryarif ardiansyahView Answer on Stackoverflow