Ryan
Ryan

Reputation: 10049

Jquery: Enter to blur

Please check out this fiddle: http://jsfiddle.net/7wp9rs2s/ this is how far I have come with this project.

Basically, in the above fiddle you can double click on one of the 4 items and when you do, you get a textbox where you can edit it. Then you click out and it goes back to being a text. Most users wont click out but will instead press enter (like SO's comment function) so if the user presses enter, while making the edit, I want the script to react in the same way as if the user clicked out of the box.

I know how to capture the Enter key:

$(document).keypress(function(e) {
  if(e.which == 13) {
    alert("in Enter");
  }
});

But I do not know how to make the function do what I need :(

Upvotes: 0

Views: 39

Answers (1)

Karl-André Gagnon
Karl-André Gagnon

Reputation: 33870

Your functions are called on blur, so simply trigger a blur on the input :

$(document).keypress(function(e) {
  if(e.which == 13) {
    $(e.target).blur();
  }
});

Fiddle

Upvotes: 1

Related Questions