1

I'm trying to fire a click event on my select options.

This is working fine in FF/Safari if the selectlist is a normal multiselect box like this:

http://jsfiddle.net/YZUBs/47/

However, if i include the jQuery Mobile framework, the click event does not trigger anymore:

http://jsfiddle.net/YZUBs/48/

Anyone?

bale3
  • 353
  • 2
  • 11
  • 19

1 Answers1

1

The click event on <option> isn't standard and thus not supported in all browsers.
You should use the onchange event instead of onclick.

You can read all the Q&A in this search, this is the subject for all of them...

Updated fiddle

Community
  • 1
  • 1
gdoron is supporting Monica
  • 136,782
  • 49
  • 273
  • 342
  • I've tried to use the change event, but then I can't find a way to know which option I just selected/unselected. – bale3 Apr 11 '12 at 09:20
  • @bale3. First, as I said you can't use the `click` on option. First You can know what is option you just selected if you save the value on each change and comparing to the previous value. "spaghetti", I know, but I'm afraid you just don't have other choice. – gdoron is supporting Monica Apr 11 '12 at 09:24
  • Hi @bale3, you can just use $(this).val() inside the event function. You can have a look at this fiddle to see how the modification would work. [modification](http://jsfiddle.net/YZUBs/53/) – txominpelu Apr 11 '12 at 09:25
  • @user433831. But if you choose two, you will get `1,2` and by the way, you can just get the value by `this.value` no `jQuery` needed... – gdoron is supporting Monica Apr 11 '12 at 09:28
  • @gdoron Thanks, will try to save the value and compare. – bale3 Apr 11 '12 at 09:37
  • @user43381, that would work if I only can select 1 option, but in my case, I have a multiselect list so if I select more than 1 option, the val() doesn't really help me. Thanks for your answer anyway =) – bale3 Apr 11 '12 at 09:37
  • @bale3 No problems. In any case you can take this [modification](http://jsfiddle.net/YZUBs/59/) to simplify saving the previous state of the select element. It's explained in http://stackoverflow.com/questions/4076770/getting-value-of-select-dropdown-before-change. – txominpelu Apr 11 '12 at 09:54
  • @user433831 Thanks, but if you look at [this updated fiddle](http://jsfiddle.net/YZUBs/60/) where I have included the jQuery Mobile framework, the focus event does not seems to work :( – bale3 Apr 11 '12 at 10:20