Wow, no really reusable solutions among answers yet.. I mean, a standart event handler should get only an event
argument and doesn't have to use ids at all.. I'd use:
function handleSelectChange(event) {
// if you want to support some really old IEs, add
// event = event || window.event;
var selectElement = event.target;
var value = selectElement.value;
// to support really old browsers, you may use
// selectElement.value || selectElement.options[selectElement.selectedIndex].value;
// like el Dude has suggested
// do whatever you want with value
}
You may use this handler with each – inline js:
<select onchange="handleSelectChange(event)">
<option value="1">one</option>
<option value="2">two</option>
</select>
jQuery:
jQuery('#select_id').on('change',handleSelectChange);
or vanilla JS handler setting:
var selector = document.getElementById("select_id");
selector.onchange = handleSelectChange;
// or
selector.addEventListener('change', handleSelectChange);
And don't have to rewrite this for each select
element you have.
Example snippet:
function handleSelectChange(event) {
var selectElement = event.target;
var value = selectElement.value;
alert(value);
}
<select onchange="handleSelectChange(event)">
<option value="1">one</option>
<option value="2">two</option>
</select>
value
of theoption
that was selected then...<select onchange="window.alert(this.value);">
...option
s...</select>
... should get ya just that and nothing more. – S0AndS0