html - Get index of selected option with jQuery


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:

        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>

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.

All Answers
  • Translate

    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:



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


  • Translate

    Good way to solve this in Jquery manner

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

  • Translate

    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:


  • Translate

    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);

  • Translate

    try this


  • Translate

    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().

  • Translate

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

    Check This :

    <!doctype html>
    <script type="text/javascript" src = ""></script>
    <script type="text/javascript">
    function check(){
    <body bgcolor="yellow">
    <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>