I have a dropdown menu with different option groups. If someone selects an option, how can I check which optgroup it belongs to? For example if \'ferrari\' were selected, how wo
Well, in pure js:
this.options[this.selectedIndex].parentNode.label
Not a single function call and less code to boot. :-)
You can do this using jQuery:
$('select').change(function() {
var selected = $(':selected', this);
alert(selected.closest('optgroup').attr('label'));
});
See a live example here: http://jsfiddle.net/jkeyes/zjLCp/1/
Update: Yes you could use parent
http://jsfiddle.net/jkeyes/zjLCp/2/
selected.parent()