71

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.

Vaidas
  • 968
  • 9
  • 22

13 Answers13

80

I don't believe the click event is valid on options. It is valid, however, on select elements. Give this a try:

$("select#yourSelect").change(function(){
    process($(this).children(":selected").html());
});
Samantha Branham
  • 7,350
  • 2
  • 32
  • 44
  • 1
    I tried to listen to the click event on options and it worked: $(document).on("click","select option",function() {console.log("nice to meet you, console ;-)"); }); – zuluk Oct 24 '14 at 20:59
  • 1
    @zuluk Even if the click event works, the change event is much more appropriate (think about what will happen if the user selects an option using his keyboard) – lukasgeiter Oct 24 '14 at 21:07
  • 16
    @lukasgeiter However what is the solution if you also want to fire an event if the user clicks on the already selected option? Change does not work... – zuluk Oct 24 '14 at 21:14
  • 2
    @zuluk Okay that's true. Although this sounds like a rather rare use case to me. Maybe add a comment about it to your answer? :) – lukasgeiter Oct 24 '14 at 21:18
  • Event works perfectly in firefox, I've working in firefox but option click event not works in chrome – Sagar Gautam Feb 06 '19 at 04:33
  • option click event works in chrome and FF but not old IE these days. Unfortunately select change event has a bug and doesn't always work in Chrome. – Jereme May 20 '20 at 19:03
  • `change()` does not work if you reselect already selected option... – qraqatit Apr 21 '22 at 00:40
22

We can achieve this other way despite of directly calling event with <select>.

JS part:

$("#sort").change(function(){

    alert('Selected value: ' + $(this).val());
});

HTML part:

<select id="sort">
    <option value="1">View All</option>
    <option value="2">Ready for Review</option>
    <option value="3">Registration Date</option>
    <option value="4">Last Modified</option>
    <option value="5">Ranking</option>
    <option value="6">Reviewed</option>
</select>
Vaidas
  • 968
  • 9
  • 22
vivek sharma
  • 231
  • 2
  • 2
13

The easy way to change the select, and update it is this.

// BY id
$('#select_element_selector').val('value').change();

another example:

//By tag
$('[name=selectxD]').val('value').change();

another example:

$("#select_element_selector").val('value').trigger('chosen:updated');
DarckBlezzer
  • 4,578
  • 1
  • 41
  • 51
  • 1
    Yes this works, but you should note that in the `.val('value')` the `'value'` is a value of `option` `value` attribute. Example: ` – mr NAE Feb 02 '19 at 15:10
7

I've had simmilar issue. change event was not good for me because i've needed to refresh some data when user clicks on option. After few trials i've got this solution:

$('select').on('click',function(ev){
    if(ev.offsetY < 0){
      //user click on option  
    }else{
      //dropdown is shown
    }
});

I agree that this is very ugly and you should stick with change event where you can, but this solved my problem.

Halon
  • 106
  • 1
  • 2
  • this is nice, but it doesn't work in Mac Chrome. The issue, when close – Oscar Zhang Apr 26 '18 at 00:39
5

I found that the following worked for me - instead on using on click, use on change e.g.:

 jQuery('#element select').on('change',  (function() {

       //your code here

}));
Elina Lulle
  • 143
  • 3
  • 14
3
<select id="myselect">
    <option value="0">sometext</option>
    <option value="2">Ready for Review</option>
    <option value="3">Registration Date</option>
</select>

$('#myselect').change(function() {
    if($('#myselect option:selected').val() == 0) {
    ...
    }
    else {
    ...
    }
});
intraector
  • 994
  • 10
  • 20
1

Looking for this on 2018. Click event on option tag, inside a select tag, is not fired on Chrome.

Use change event, and capture the selected option:

$(document).delegate("select", "change", function() {
    //capture the option
    var $target = $("option:selected",$(this));
});

Be aware that $target may be a collection of objects if the select tag is multiple.

1

I use a two part solution

  • Part 1 - Register my click events on the options like I usually would
  • Part 2 - Detect that the selected item changed, and call the click handler of the new selected item.

HTML

<select id="sneaky-select">
  <option id="select-item-1">Hello</option>
  <option id="select-item-2">World</option>
</select>

JS

$("#select-item-1").click(function () { alert('hello') });
$("#select-item-2").click(function () { alert('world') });

$("#sneaky-select").change(function ()
{
   $("#sneaky-select option:selected").click();
});
Aaron Sherman
  • 3,789
  • 1
  • 30
  • 33
0

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

pardahlman
  • 1,394
  • 10
  • 22
0

Maybe one of the new jquery versions supports the click event on options. It worked for me:

$(document).on("click","select option",function() {
  console.log("nice to meet you, console ;-)");
});

UPDATE: A possible usecase could be the following: A user sends a html form and the values are inserted into a database. However one or more values are set by default and you flag this automated entries. You also show the user that his entry is generated automatically, but if he confirm the entry by clicking on the already selected option you change the flag in the database. A rare sue case, but possible...

zuluk
  • 1,557
  • 8
  • 29
  • 49
  • Just as a note, using `$(document)` can pollute the `document` click events, especially when debugging on the Chrome dev Tools (for example). I prefer (in this case, for example) to use `$('select option')` (in other words, assign the event directly to the element). – edmundo096 May 16 '16 at 16:41
0

I know that this code snippet works for recognizing an option click (at least in Chrome and FF). Furthermore, it works if the element wasn't there on DOM load. I usually use this when I input sections of inputs into a single select element and I don't want the section title to be clicked.

$(document).on('click', 'option[value="disableme"]', function(){
    $('option[value="disableme"]').prop("selected", false);
});
Gwi7d31
  • 1,652
  • 2
  • 12
  • 10
0

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()

Ilan Schemoul
  • 1,461
  • 12
  • 17
0

Workaround:

$('#select_id').on('change', (function() { $(this).children(':selected').trigger('click'); }));

JeanP
  • 36
  • 6