Get index of select option javascript

I have sầu a normal dropdown which I want to get the currently selected index và put that in a variable. Jquery or javascript. Jquery perfered.

Bạn đang xem: Get index of select option javascript

$("select option:selected") should do the trick

See jQuery documentation for more detail:

UPDATE: if you need the index of the selected option, you need to lớn use the .index() jquery method:

$("select option:selected").index()
Calling index() is orders of magnitude more list-searching busy-work than just using standard DOM selectedIndex và no more readable. I'd stichồng with the plain DOM property here. – bobince Sep 2 "10 at 15:00

bobince: I wouldn't use jQuery to solve this question either, but as the OPhường asked for a jQuery solution... :-) – naivists Sep 2 "10 at 15:02
This was the simplest & exactly what I was looking for. The second line works perfect i used it lượt thích var indx = $("select option:selected").index(); – user357034 Sep 2 "10 at 15:02


If you are actually looking for the index number (& not the value) of the selected option then it would be

document.forms<0>.elements<"CCards">.selectedIndex /* You may need to lớn change document.forms<0> to reference the correct khung */

or using jQuery


the actual index is available as a property of the select element.

var sel = document.getElementById("CCards"); alert(sel.selectedIndex);

you can use the index to get khổng lồ the selection option, where you can pull the text và value.

Xem thêm: 19 Best Php Ide And Code Editors In 2021, List Of Php Editors

var opt = sel.options; alert(opt.text); alert(opt.value);

You can also use :checked for