8

Using JQuery or JavaScript, I want to detect when a user selects a value, even if they don't change the already selected value.

How can this be accomplished?

I tried -

$('#MyID').select(function(){ /*my function*/ });

and

$('#MyID').change(function(){ /*my function*/ }); //nothing changing, so this fails

But they do not work.

Example - I have a dropdown with a list of years in it with nothing selected, the user selects "1976", I run a function. With "1976" selected, the user clicks on the dropdown again and selects "1976" again, I want to run the function again.

kasper Taeymans
  • 6,950
  • 5
  • 32
  • 51
A Bogus
  • 3,852
  • 11
  • 39
  • 58
  • 4
    You can't, as selecting the same value isn't a change, it's the same. Maybe you could just listen for any click on the element, and that's about as close as you'll get without hacking away at the default browser behaviour. – adeneo Jun 26 '13 at 17:50
  • Maybe try listening for a click on `option`? – ayyp Jun 26 '13 at 17:52
  • @AndrewPeacock - option elements don't fire mouse events in all browsers. – adeneo Jun 26 '13 at 17:53
  • @adeneo Hmm, didn't know that. Maybe listen on `select` and track if it's closed? Or do custom events and pull the value in there? – ayyp Jun 26 '13 at 17:55
  • @AndrewPeacock Addtitionally, if they could be using they keyboard. – Pete Jun 26 '13 at 17:59
  • 4
    Here's the answers to your question: http://stackoverflow.com/a/898761/1648170 http://stackoverflow.com/questions/11002421/jquery-event-to-fire-when-a-drop-down-is-selected-but-the-value-is-not-change – cbelizon Jun 26 '13 at 18:07
  • 1
    Why run the function again when the value did not change? there is probably a better approach to this – Huangism Jan 16 '14 at 18:36
  • As @user1648170 has indicated the answer can be found here http://stackoverflow.com/questions/11002421/jquery-event-to-fire-when-a-drop-down-is-selected-but-the-value-is-not-change – Ben Smith Jan 17 '14 at 00:56
  • cant you just handle `click` event? – Deepak Ingole Jan 21 '14 at 14:09
  • possible duplicate of [Check whether some element is "focused" without jQuery](http://stackoverflow.com/questions/7052600/check-whether-some-element-is-focused-without-jquery) – Paul Sweatte Jan 27 '14 at 23:16

7 Answers7

6

all systems go (on second click at least...)

Set a switch to run on selection, and reset the switch after it's captured and/or on blur.

var go = false;//switch off
$('#MyID').on('click',function() {//on click
    if (go) {//if go
        //do stuff here with $(this).val()
        go = false;//switch off
    } else { go = true; }//if !go, switch on
}).on('blur', function() { go = false; });//switch off on blur

made a fiddle: http://jsfiddle.net/filever10/2XBVf/

edit: for keyboard support as well, something like this should work.

var go = false;//switch off
$('#MyID').on('focus active click',function() {//on focus/active
    doit($(this));//do it
}).on('change', function() {
    go=true;//go
    doit($(this));//and doit
}).on('blur', function() { go = false; });//switch off on blur

function doit(el) {
    if (go) {//if go
        //do stuff here with el.val()
        go = false;//switch off
    } else {go=true}//else go
}

made a fiddle: http://jsfiddle.net/filever10/TyGPU/

FiLeVeR10
  • 2,155
  • 1
  • 12
  • 13
  • 2
    Correct, if OP had specified that I would've done something more like [this](http://jsfiddle.net/filever10/TyGPU/) – FiLeVeR10 Feb 13 '14 at 21:34
0

Have you tried .blur()?

$('#MyID').blur(function(){
    //my function
});

Please note that you will have to click somewhere outside the dropdown menu on the page before the function will trigger. I don't know if this can be passed.

JsFiddle here

Petr R.
  • 1,247
  • 2
  • 22
  • 30
  • You can also try `.focusin()` and `.blur()`, it's working for me in Chrome (see the fiddle). – Petr R. Jun 26 '13 at 18:14
  • When I select the same value that's already selected noting happens for me. – A Bogus Jun 26 '13 at 18:18
  • Now I'm not sure I understand the question correctly. You want to trigger the function even if the user clicks on same field multiple times [like this](http://jsfiddle.net/xuwyc/1)? Or you want to trigger the function when someone *"selects"* the box and goes away without changing anything (that's what my original answer was intendet to do)? Thanks for clarification. – Petr R. Jun 26 '13 at 18:30
  • 1
    I guess the best way to explain it is by example - I have a dropdown with a list of years in it with nothing selected, the user selects "1976", I run a function. If the user clicks on the dropdown again and selects "1976" AGAIN, I want to run the function again. – A Bogus Jun 26 '13 at 18:34
  • Thanks a lot, I get it now. I updated my answer once more. `.blur()` seems to be the closest solution I know about, but there still is some delay before the function is triggered. – Petr R. Jun 26 '13 at 18:56
0

Try this...

To get selected option's value...

$("body").click(function(event) {
    if(event.target.nodeName== "SELECT"){
                 selectedValue = event.target.value;
            }   
});

and then get the text from the value

var text = $("option").filter(function() {
      return $(this).attr("value") === selectedValue;
    }).first().text();
Premshankar Tiwari
  • 3,006
  • 3
  • 24
  • 30
0

Have you tried something like:

$('#MyID li').click(function(){ //my function });

Here you are detecting clicks on the list elements inside your dropdown, which should be the different options. (If your HTML is slightly different, just inspect it and see what all the options have in common, what kind of elements are they?)

quantka
  • 920
  • 1
  • 10
  • 15
  • Nevermind...I just realized this html was due to a plugin I was using, and I tried using the option elements like `$('#MyID option').click(...)` but that doesn't work, sorry! – quantka Jan 22 '14 at 20:48
0
$('#MyID option').hover(function(){
   //user is on an option, but not selected it yet
},function(){
   //user left an option without selecting
}).click(function(){
   //user clicked on an option, selecting it

});
Sumit Kumar
  • 1,855
  • 19
  • 19
0

Try this code.

$("#MyID").click(function (){
    alert($("#MyID").val());
});

Try Here!

HeiN
  • 503
  • 3
  • 17
0

I've made a working jsfiddle only tested on firefox. I've managed to do what you want by adding a dummy option to the selectbox. This dummy option has a display:none style and won't be visible in the option list.

DEMO

<select id="myselect">
    <option value="1">one</option>
    <option value="2">two</option>
    <option value="3" selected="selected">three</option>
    <option value="4">four</option>
    <option class="dummy" style="display:none">dummy</option>
</select>



$('#myselect').on('focus',function(){    
    $(this).find("option.dummy").prop("selected", true);
});

$('#myselect').on('change',function(){
    var select=$(this);
    var text=select.find("option:selected").text();

    $('.dummy').attr('value',select.val()).text(text);
    alert(select.val());


});
kasper Taeymans
  • 6,950
  • 5
  • 32
  • 51