snippet

Find index of selected option

jQuery code snippet to find the index of the option that has been selected from within a select element.

$('select').change(function() {
  $('#output').text( $(this).children().index( $(this).children(':selected') ) );
}).trigger('change');

Use jQuery's index function to return the index of the item that matches the ':selected' selector within the children of the select element.

Example:

Syndicate content