Get index of selected option with jQuery

JqueryHtmlSelectIndexing

Jquery Problem Overview


I'm a little bit confused about how to get an index of a selected option from a HTML <select> item.

On this page there are two methods described. However, both are always returning -1. Here is my jQuery code:

$(document).ready(function(){
	$("#dropDownMenuKategorie").change(function(){
		alert($("#dropDownMenuKategorie option:selected").index());
		alert($("select[name='dropDownMenuKategorie'] option:selected").index());
	});
});

and in html

(...)
<select id="dropDownMenuKategorie">
	<option value="gastronomie">Gastronomie</option>
	<option value="finanzen">Finanzen</option>
	<option value="lebensmittel">Lebensmittel</option>
	<option value="gewerbe">Gewerbe</option>
	<option value="shopping">Shopping</option>
	<option value="bildung">Bildung</option>
</select>
(...)

Why this behavior? Is there any chance that the select is not "ready" at the moment of assigning its change() method? Additionally, changing .index() to .val() is returning the right value, so that's what confuses me even more.

Jquery Solutions


Solution 1 - Jquery

The first methods seem to work in the browsers that I tested, but the option tags doesn't really correspond to actual elements in all browsers, so the result may vary.

Just use the selectedIndex property of the DOM element:

alert($("#dropDownMenuKategorie")[0].selectedIndex);

###Update:

Since version 1.6 jQuery has the prop method that can be used to read properties:

alert($("#dropDownMenuKategorie").prop('selectedIndex'));

Solution 2 - Jquery

Good way to solve this in Jquery manner

$("#dropDownMenuKategorie option:selected").index()

Solution 3 - Jquery

You can use the .prop(propertyName) function to get a property from the first element in the jQuery object.

var savedIndex = $(selectElement).prop('selectedIndex');

This keeps your code within the jQuery realm and also avoids the other option of using a selector to find the selected option. You can then restore it using the overload:

$(selectElement).prop('selectedIndex', savedIndex);

Solution 4 - Jquery

I have a slightly different solution based on the answer by user167517. In my function I'm using a variable for the id of the select box I'm targeting.

var vOptionSelect = "#productcodeSelect1";

The index is returned with:

$(vOptionSelect).find(":selected").index();

Solution 5 - Jquery

try this

 alert(document.getElementById("dropDownMenuKategorie").selectedIndex);

Solution 6 - Jquery

selectedIndex is a JavaScript Select Property. For jQuery you can use this code:

jQuery(document).ready(function($) {
  $("#dropDownMenuKategorie").change(function() {
    // I personally prefer using console.log(), but if you want you can still go with the alert().
    console.log($(this).children('option:selected').index());
  });
});

Solution 7 - Jquery

You can get the index of the select box by using : .prop() method of JQuery

Check This :

<!doctype html>
<html>
<head>
<script type="text/javascript" src = "http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){

});

function check(){
	alert($("#NumberSelector").prop('selectedIndex'));
	alert(document.getElementById("NumberSelector").value);
}
</script>
</head>

<body bgcolor="yellow">
<div>
<select id="NumberSelector" onchange="check()">
	<option value="Its Zero">Zero</option>
	<option value="Its One">One</option>
	<option value="Its Two">Two</option>
	<option value="Its Three">Three</option>
	<option value="Its Four">Four</option>
	<option value="Its Five">Five</option>
	<option value="Its Six">Six</option>
	<option value="Its Seven">Seven</option>
</select>
</div>
</body>
</html>

Solution 8 - Jquery

Actually just reiterating what has already been stated a little differently:

$("#dropDownMenuKategorie").change(function() {
     var Selection = $("#dropDownMenuKategorie option:selected");
     alert(Selection.index());
     alert(Selection.val());
});

Solution 9 - Jquery

Assume You have jquery loaded. So

HTML :

<select id="dropDownMenuKategorie">
    <option value="gastronomie">Gastronomie</option>
    <option value="finanzen">Finanzen</option>
    <option value="lebensmittel">Lebensmittel</option>
    <option value="gewerbe">Gewerbe</option>
   <option value="shopping">Shopping</option>
   <option value="bildung">Bildung</option>
</select>

JS:

$(document).ready(function(){
    $("#dropDownMenuKategorie").change(function(){
       var selIndex = $(this).prop('selectedIndex');
       var selVal = $("#dropDownMenuKategorie option:selected").val();
       var selText = $("#dropDownMenuKategorie option:selected").text();
       console.log(selIndex + selVal + selText );
    });
});

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
QuestionValentino RuView Question on Stackoverflow
Solution 1 - JqueryGuffaView Answer on Stackoverflow
Solution 2 - Jqueryuser167517View Answer on Stackoverflow
Solution 3 - JqueryNick BedfordView Answer on Stackoverflow
Solution 4 - JqueryluckychiiView Answer on Stackoverflow
Solution 5 - Jqueryrajesh kakawatView Answer on Stackoverflow
Solution 6 - JqueryRishi KulshreshthaView Answer on Stackoverflow
Solution 7 - JqueryNiharika BirariView Answer on Stackoverflow
Solution 8 - JqueryRationalRabbitView Answer on Stackoverflow
Solution 9 - JqueryinfomasudView Answer on Stackoverflow