adivasile
adivasile

Reputation: 2487

Change event on <select>

With Mootools, if I attach a change event listener on a <select> how do I access the option that was selected. I would like the actual element and not just the value.

$('select').addEvent('change',function(event) {
    //??
});

Upvotes: 11

Views: 15232

Answers (3)

Chris Walsh
Chris Walsh

Reputation: 3523

event.target.id is the object

event.target.value is the new value

Upvotes: 0

Andy E
Andy E

Reputation: 344675

Just access the selectedIndex property on the select element (this object in the event handler) to get the option index.

// get the index of the selected option
var index = this.selectedIndex;

// get the option element
var opt   = this.options[index];

Upvotes: 4

Anurag
Anurag

Reputation: 141889

Either of these will work:

find by :selected pseudo selector in descendants

this.getElement(':selected');

get first selected value

this.getSelected()[0];

pure javascript, use the selectedIndex property

this.options[this.selectedIndex];

Upvotes: 13

Related Questions