I\'m having a problem in Chrome
with the following:
var items = $(\"option\", obj);
items.each(function(){
$(this).click(function(){
Since $(this)
isn't correct anymore with ES6 arrow function which don't have have the same this
than function() {}
, you shouldn't use $( this ) if you use ES6 syntax.
Besides according to the official jQuery's anwser, there's a simpler way to do that what the top answer says.
The best way to get the html of a selected option is to use
$('#yourSelect option:selected').html();
You can replace html()
by text()
or anything else you want (but html()
was in the original question).
Just add the event listener change
, with the jQuery's shorthand method change()
, to trigger your code when the selected option change.
$ ('#yourSelect' ).change(() => {
process($('#yourSelect option:selected').html());
});
If you just want to know the value of the option:selected
(the option that the user has chosen) you can just use $('#yourSelect').val()