dotty
dotty

Reputation: 41473

Index of the item curent being show in a <select> box

I have a select box with the following data

<select>
    <option>Bill</option>
    <option>Ted</option>
    <option>Bogus</option>
</select>

When i select "Bogus" i want to return 2 back to a function, because this is the third item.

Any idea how to do this with jQuery? Something like

jQuery("#colour").change(function(){
    alert( $(this).index );         
});

doesn't work.

Thanks

Upvotes: 1

Views: 75

Answers (2)

JaredMcAteer
JaredMcAteer

Reputation: 22535

You can also modify your jquery to use the index method

$('option:selected', this).index()

http://jsfiddle.net/rcFGj/

Upvotes: 0

Lukas Eder
Lukas Eder

Reputation: 221106

After all this jquery business, we forget how it was done before... ;-)

It's

this.selectedIndex

Upvotes: 5

Related Questions