Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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) {
    //??
});
like image 357
adivasile Avatar asked Mar 17 '10 09:03

adivasile


Video Answer


2 Answers

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];
like image 159
Anurag Avatar answered Nov 16 '22 00:11

Anurag


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];
like image 34
Andy E Avatar answered Nov 16 '22 01:11

Andy E