70

given:

<select id="mySelect">
  <option>..</option>
  ...
</select>

Using the select id, how can I trigger a click event on one of the options? I tried attaching the event directly to the select, but this triggers an event whenever the select is clicked on (even if there are no options). Oh, and it is a multi-select (although I don't think that matter).

john
  • 2,353
  • 6
  • 21
  • 14
  • Good answer here when needing the value before the change event http://stackoverflow.com/questions/4076770/getting-value-of-select-dropdown-before-change – malkoty Oct 09 '14 at 16:48

8 Answers8

80

You want the 'change' event handler, instead of 'click'.

$('#mySelect').change(function(){ 
    var value = $(this).val();
});
Matt
  • 38,720
  • 26
  • 105
  • 143
  • 35
    what if they choose the same option? the change event is not triggered – haz0rd Sep 26 '13 at 21:03
  • 1
    @haz0rd you will only be able to detect that with the click event, of which you will get 2. 1 when you first click the select, and 1 when you select the already selected option – Matt Sep 27 '13 at 01:13
  • 17
    Please note `click` events do not *currently* get generated in Chrome at all for ` – Gone Coding Feb 19 '15 at 09:28
  • @GoneCoding Is there any way around the `click` not firing in Chrome? – Thomas Valadez Jun 29 '17 at 22:08
  • @ThomasValadez: Use a custom select, not a real select. – Gone Coding Jul 03 '17 at 17:40
  • 1
    Its not what he asked for or what he "wants". He asked for an event that triggers on a click on one of the options. Your answer is a workaround that wont't trigger when click on default or same option – Mbotet Mar 18 '19 at 13:42
  • Change handler doesn't work well because it triggers on every keypress on keyboard navigation with cursor keys instead of just on the press of enter – Matthew Dolman Sep 10 '19 at 01:45
43

$('#mySelect').on('change', function() {
  var value = $(this).val();
  alert(value);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
<select id="mySelect">
  <option value='1'>1</option>
  <option value='2'>2</option>
  <option value='3'>3</option>
  <option value='4'>4</option>
  <option value='5'>5</option>
  <option value='6'>6</option>
  <option value='7'>7</option>
  <option value='8'>8</option>
</select>

EXAMPLE

Ivošš
  • 959
  • 10
  • 17
5

you can attach a focus event to select

$('#select_id').focus(function() {
    console.log('Handler for .focus() called.');
});
tbradley22
  • 1,415
  • 2
  • 15
  • 14
0

The problem that I had with the change handler was that it triggered on every keypress that I scrolled up and down the <select>.

I wanted to get the event for whenever an option was clicked or when enter was pressed on the desired option. This is how I ended up doing it:

let blockChange = false;

$element.keydown(function (e) {

    const keycode = (e.keyCode ? e.keyCode : e.which);

    // prevents select opening when enter is pressed
    if (keycode === 13) {
        e.preventDefault();
    }

    // lets the change event know that these keypresses are to be ignored
    if([38, 40].indexOf(keycode) > -1){
        blockChange = true;
    }

});

$element.keyup(function(e) {

    const keycode = (e.keyCode ? e.keyCode : e.which);
    // handle enter press
    if(keycode === 13) {
        doSomething();
    }

});

$element.change(function(e) {

    // this effective handles the click only as preventDefault was used on enter
    if(!blockChange) {
        doSomething();
    }
    blockChange = false;

});
Matthew Dolman
  • 1,622
  • 6
  • 24
  • 46
0

You can also try this to get previous value of select and the clicked value.

HTML

<select name="status">
    <option value="confirmed">confirmed</option>
    <option value="packed">packed</option>
    <option value="shiped">shiped</option>
    <option value="delivered">delivered</option>
</select>

script


    var previous;

    $("select[name=status]").focus(function () {
        previous = this.value;
    }).change(function() {
        var next = $(this).val()
        
        alert("previous: "+previous+ " and Next: "+ next)
        
        previous = this.value;
    });


-1

What I have done in this situation is that I put in the option elements OnClick event like this:

<option onClick="something();">Option Name</option>

Then just create a script function like this:

function something(){
    alert("Hello"); 
    }

UPDATE: Unfortunately I can't comment so I'm updating here
TrueBlueAussie apparently jsfiddle is having some issues, check here if it works or not: http://js.do/code/klm

Xm7X
  • 863
  • 11
  • 22
Igor Aleksic
  • 125
  • 5
  • 1
    This does not *currently* work with Chrome so should be avoided: http://jsfiddle.net/TrueBlueAussie/jd3bamtr/1/ – Gone Coding Feb 19 '15 at 09:27
  • 1
    p.s your code lacks the javascript code so....next time at least make sure you have everything implemented to someones suggestions and checked via "Chromes" debugger that its not working and why !!before warning other users!! that its not working and downvoting ;) – Igor Aleksic Feb 19 '15 at 14:07
  • The only Javascript required for that test *was inline*. If you actually bother to put your ` – Gone Coding Feb 19 '15 at 14:49
-1

its working for me

<select name="" id="select">
    <option value="1"></option>
    <option value="2"></option>
    <option value="3"></option>
</select>

<script>
    $("#select > option").on("click", function () {
       alert(1)
    })
</script>
  • Please consider to add an explanation to the answer in order for others to understand the logic – idan May 23 '20 at 07:51
-2

One possible solution is to add a class to every option

<select name="export_type" id="export_type">
    <option class="export_option" value="pdf">PDF</option>
    <option class="export_option" value="xlsx">Excel</option>
    <option class="export_option" value="docx">DocX</option>
</select>

and then use the click handler for this class

$(document).ready(function () {

    $(".export_option").click(function (e) {
        //alert('click');
    });

});

UPDATE: it looks like the code works in FF, IE and Opera but not in Chrome. Looking at the specs http://www.w3.org/TR/html401/interact/forms.html#h-17.6 I would say it's a bug in Chrome.

VladL
  • 11,959
  • 10
  • 58
  • 82
  • @TrueBlueAussie just tested your fiddle in FF and IE and it works as expected – VladL Feb 18 '15 at 19:30
  • @TrueBlueAussie and even in the old good Opera 12 it does work! – VladL Feb 18 '15 at 19:31
  • @TrueBlueAussie I understand, you've tested it just in chrome and I tested it everywhere else besides chrome. Otherwise you should write "it doesn't work in crome" instead of "Click events are eaten by the drop down" which is obviously incorrect. See my update – VladL Feb 19 '15 at 07:17
  • That is impractical. If it does not work in one of the most popular browsers then it is *not* a practical solution. It is not *my* responsibility to test *your* answer in all browsers. That would be yours :) – Gone Coding Feb 19 '15 at 08:03
  • @TrueBlueAussie have you looked at the w3 specs for the option tag? It supports an onclick event, but it doesn't fire in chrome right now( now is important word as chromium claims to completely implement w3 standart). So you did a very good job at finding a bug in chrome. Thanks for finding the time to test and down vote my answer, hopefully you have some spare time to fill a bug report for chromium as well. Have a good day :) – VladL Feb 19 '15 at 09:17
  • When you reference a W3 spec, please provide a link as there are multiple versions relating to past and future releases. Until the Chrome issue/bug is "fixed" your answer will not lead to working cross-browser solution, so sarcasm is pointless. *This answer does not currently "work"* and should be avoided in the real world of cross-browser development. That is just being practical. If the answer ever becomes "correct" I will up-vote instead. Can't say fairer than that. – Gone Coding Feb 19 '15 at 09:25
  • @TrueBlueAussie Just scroll back to the comment there I write "See my update" and then scroll to my answer, I've described everything before being sarcastic. Sure thing, my solution is not working in chrome now(!) but reading your first comment one can think that my solution is completely wrong which is not true. It's just a bug in chrome. Just be honest, do you test all your answers in Chrome, FF, IE, Opera and Safari before posting? – VladL Feb 19 '15 at 09:40
  • Feel free to review my answers for cross-browser issues. I try to be aware of these issues, but mistakes happen. I feel you are still missing my point though, as this "bug" is widely known (and used to apply to more browsers). Sure, it may be a "browser bug", but "browser bugs" *must* be avoided, at all costs (unless you feel it is OK for code to work on "most browsers"). – Gone Coding Feb 19 '15 at 09:48
  • 1
    *Previous ambiguous comment removed*. Just to clarify for the pedantic: This solution *does not* work for Chrome *as of this time of writing*: http://jsfiddle.net/TrueBlueAussie/jd3bamtr – Gone Coding Feb 19 '15 at 09:50
  • @TrueBlueAussie "as this "bug" is widely known (and used to apply to more browsers)". Do I understand you right and you can tell me another browser besides chrome where onclick event on option tag is not executed as well? Or do you mean something else? – VladL Feb 19 '15 at 10:26
  • *"used to"* means *in the past*. – Gone Coding Feb 19 '15 at 10:37