-1

In my application I have one button and one text box. Now I have call one function of jQuery on click and keypress event of button.

now i want that the same function is call when the ENTER key press on text box.

is there any way to call that jQuery function?

I have not form this both elements are work without form

Uday A. Navapara
  • 1,237
  • 5
  • 20
  • 40

3 Answers3

2

Try this

$('#search_btn').keypress(function(e) {
    if(e.which == 13) {

        alert("Enter pressed");
       //Call your function here, or trigger the button click.
    }
});

DEMO: http://jsfiddle.net/skhan/brdaj/

Dot_NET Pro
  • 2,095
  • 2
  • 21
  • 38
1

you can use keypress() with jquery, like this :

$(document).keypress(function(e) {
    if(e.which == 13) {
        console.log("2");
    }
});

Check FIDDLE

If you dont want to make with jquery,(form onsubmit or javascript) check this source :

Detect enter key pressed using javascript

and this one;

Enter key press event in JavaScript

Community
  • 1
  • 1
mehmetakifalp
  • 445
  • 5
  • 16
1

This should do what you want:

$('body').keyup(function(e){
   if(e.which == 13 && $('#yourText').is(':focus')){
     console.log("do your thing");
   }
});

http://jsfiddle.net/L2M3Y/1/

shredmill
  • 283
  • 3
  • 10