1

I have a table and I use select menu in each row for different actions for that specific row.

For example:

$(document).on('change', '.lead-action', function() {
    // Do stuff
}

this method gets the value of the selected option. Based on the selected value, I display different popups. When the user leaves the page, the select menu retains the previously selected option.

Sometimes users click on the same option in the select menu. When they do, the above code doesn't work.

Is there a way to invoke the code block above if the same option in the select menu is selected?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
u54r
  • 1,767
  • 2
  • 15
  • 26
  • Why don't you trigger change event once you have selected the relevant option programatically? – A. Wolff Apr 24 '14 at 19:35
  • Can you explain it more? When you click on the select menu the option is already selected because previously that action is assigned to the row. – u54r Apr 24 '14 at 19:40
  • @A.Wolff, imagine a countries menu and United States is selected. When you choose United States again from the select menu, nothing will be triggered because nothing is changed. What I want is if you select Unites States again I want to trigger an event. – u54r Apr 24 '14 at 19:48

2 Answers2

4

I'm gathering that you just want the dropdown to fire anytime a selection is made. If so, check out the answer to Fire event each time a DropDownList item is selected with jQuery.

See my updated answer below:
You can use this small extension:

$.fn.selected = function(fn) {
    return this.each(function() {
        var clicknum = 0;
        $(this).click(function() {
            clicknum++;
            if (clicknum == 2) {
                clicknum = 0;
                fn(this);
            }
        });
    });
}

Then call like this:

$(".lead-action").selected(function(e) {
    alert('You selected ' + $(e).val());
});

Update:

I'm actually rather unhappy with the original script. It will break in a lot of situations, and any solution that relies on checking the click count twice will be very fickle.

Some scenarios to consider:

  • If you click on, then off, then back on, it will count both clicks and fire.
  • In firefox, you can open the menu with a single mouse click and drag to the chosen option without ever lifting up your mouse.
  • If you use any combination of keyboard strokes you are likely to get the click counter out of sync or miss the change event altogether.
    • You can open the dropdown with Alt+ (or the Spacebar in Chrome and Opera).
    • When the dropdown has focus, any of the arrow keys will change the selection
    • When the dropdown menu is open, clicking Tab or Enter will make a selection

Here's a more comprehensive extension I just came up with:

The most robust way to see if an option was selected is to use the change event, which you can handle with jQuery's .change() handler.

The only remaining thing to do is determine if the original element was selected again.
This has been asked a lot (one, two, three) without a great answer in any situation.

The simplest thing to do would be to check to see if there was a click or keyup event on the option:selected element BUT Chrome, IE, and Safari don't seem to support events on option elements, even though they are referenced in the w3c recommendation

Inside the Select element is a black box. If you listen to events on it, you can't even tell on which element the event occurred or whether the list was open or not.

The next best thing is to handle the blur event. This will indicate that the user has focused on the dropdown (perhaps seen the list, perhaps not) and made a decision that they would like to stick with the original value. To continue handling changes right away we'll still subscribe to the change event. And to ensure we don't double count, we'll set a flag if the change event was raised so we don't fire back twice:

Updated example in jsFiddle

(function ($) {
    $.fn.selected = function (fn) {
        return this.each(function () {
            var changed = false;
            $(this).focus(function () {
                changed = false;
            }).change(function () {
                changed = true;
                fn(this);
            }).blur(function (e) {
                if (!changed) {
                    fn(this);
                }
            });
        });
    };
})(jQuery);
Community
  • 1
  • 1
KyleMit
  • 30,350
  • 66
  • 462
  • 664
  • The code is little bit buggy. It fires when you right click the select menu. It fires when you click on the select menu then click on empty area on the page. – u54r Apr 30 '14 at 15:07
  • @u54r, by design it's being called anytime the `select` element receives focus and loses it. So by right clicking it and then clicking away from it, you are triggering that. It is not a perfect sync with the change event, but seems to line up well if you just wanted to know if the user ever go near this element. For reasons in my answer, it seems difficult to address anything beyond that. You can still use the original answer, but I believe it's even buggier. – KyleMit Apr 30 '14 at 15:11
1

Instead of relying on change() for this use mouseup() -

$(document).on('mouseup', '.lead-action', function() {
    // Do stuff
}

That way, if they re-select, you'll get an event you can handle.

http://jsfiddle.net/jayblanchard/Hgd5z/

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
  • I don't think this is the right thing to do. It fires up as soon as I click on the select menu – u54r Apr 24 '14 at 19:30
  • Why? You're going to get a mouseup event every time you select something in a drop-down, even if you select something you selected before. – Jay Blanchard Apr 24 '14 at 19:31
  • Appreciate the jsfiddle but it's not correct. As you can see yourself, it fires as soon as you click on the select menu. – u54r Apr 24 '14 at 19:41
  • No, it fires when you release the mouse, there is a huge difference. If an option is selected it stays selected and the mouseup records the same information. If the mouse is not released the user can then move to a different answer, release the mouse and get different results. Perhaps your UI needs to be re-thought? – Jay Blanchard Apr 24 '14 at 19:44
  • This method works in Safari, but not in Chrome. In Chrome it fires right when the select button is pushed, this is the problem with the answer I proposed as well. – bigmike7801 Apr 24 '14 at 19:48
  • Actually it works in IE, Chrome, FF and Safari (on a Windows machine) - having tested them in all of them. For IE you have to comment out the console.log() statement though - it still doesn't care for that too much. – Jay Blanchard Apr 24 '14 at 19:51
  • Thanks for the explanation Jay, you're really pushing hard but this is not what I need perhaps you misunderstood what I need. PS. Down-vote wasn't me. – u54r Apr 24 '14 at 19:52
  • Well it doesn't work in Chrome or Firefox on my Mac. – bigmike7801 Apr 24 '14 at 19:54
  • If I misunderstood I apologize. Based on what you explained this is how I have coded it in many situations. If you want the popup to appear when the user selects the same option you'll have to tie it to a mouse or maybe even a keyboard event. – Jay Blanchard Apr 24 '14 at 19:55
  • I'll have to test on my Mac later @bigmike7801, but there is nothing funny in this code that would keep it from working on Chrome on that platform. Although Chrome has been known to be funny in certain versions. – Jay Blanchard Apr 24 '14 at 19:57
  • It doesn't work correctly in Chrome on my Windows machine either. You probably just have a fundamental misunderstanding of what OP is wanting. – bigmike7801 Apr 24 '14 at 20:00
  • What do you mean by "doesn't work correctly"? Are you referring to the fact that when the mouse is released, after clicking on the select, an event is generated without actually choosing an option? If so, that is correct - it is not working the way that the OP wants it. That was not clear from his original post. What I provided doesn't suit his needs, but does provide an action even when the same option is selected. EDIT: Just tested on MBP with Chrome and it works just as it does on Windows. – Jay Blanchard Apr 24 '14 at 20:03
  • Actually I mean that it generates the event when I pushdown on the mouse button and before I release it. – bigmike7801 Apr 24 '14 at 20:05
  • I have not seen it do that @bigmike7801, only when the mouse is released. – Jay Blanchard Apr 24 '14 at 20:06
  • @Jay Blanchard, Don't know why you were downvoted, so I'll just go ahead and even that out for you. :) – KyleMit Apr 24 '14 at 20:10
  • @JayBlanchard, No one should be punished for contributing to a solution. – KyleMit Apr 24 '14 at 20:12
  • I agree with that @KyleMit, which is why I upvote so much. I even leave my bad posts and take my lumps :) – Jay Blanchard Apr 24 '14 at 20:14