I'm having a problem in Chrome
with the following:
var items = $("option", obj);
items.each(function(){
$(this).click(function(){
// alert("test");
process($(this).html());
return false;
});
});
The click
event doesn't seem to fire in Chrome
, but works in Firefox
.
I wanna be able to click
on a option
element from a combo, if I do instead another kind of element, lets say <li>
it works fine. Any ideas? Thanks.
Since
$(this)
isn't correct anymore with ES6 arrow function which don't have have the samethis
thanfunction() {}
, 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
You can replace
html()
bytext()
or anything else you want (buthtml()
was in the original question).Just add the event listener
change
, with the jQuery's shorthand methodchange()
, to trigger your code when the selected option change.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()
I use a two part solution
HTML
JS
I've had simmilar issue.
change
event was not good for me because i've needed to refresh some data when user clicks onoption
. After few trials i've got this solution:I agree that this is very ugly and you should stick with
change
event where you can, but this solved my problem.I don't believe the click event is valid on options. It is valid, however, on select elements. Give this a try:
What usually works for me is to first change the value of the dropdown, e.g.
$('#selectorForOption').attr('selected','selected')
and then trigger the a change
$('#selectorForOption').changed()
This way, any javascript that is wired to