1

I have the following in javascript. I would like to fire an action when the user presses enter rather than double click. How would I go about this?

$(document).on("dblclick", ".chooser select.right", function() {
move($(this).parents(".chooser"), ".right", ".left");
});
Cœur
  • 37,241
  • 25
  • 195
  • 267
Sam Kingston
  • 817
  • 1
  • 10
  • 19
  • 1
    Presses Enter where? – Barmar Nov 24 '15 at 01:13
  • 2
    `$(document).on("keyup", ".chooser select.right", function() { ... });` and check whether the keycode is Enter. – Barmar Nov 24 '15 at 01:14
  • This question is similar to http://stackoverflow.com/questions/18160342/jquery-how-to-trigger-click-event-on-pressing-enter-key?answertab=votes#tab-top – Nick Nov 24 '15 at 01:20
  • Remove the `ondblclick` Event. Run your code inside `onsubmit` before you `return false;`, if you're using a form. – StackSlave Nov 24 '15 at 01:25

1 Answers1

2

To fire an event on a key press, change the dblclick to listen for key presses, and look for the correct key, in this case the enter key. Try something like this:

$(function(){
    $(document).on("keypress", ".chooser select.right", function(key) {
        if(key.which == 13) { // enter key
           move($(this).parents(".chooser"), ".right", ".left");
        }
    });
});

Try it out in this fiddle (remember to press the "result" window first).

Jonas Lomholdt
  • 1,215
  • 14
  • 23