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
Try this
$('#search_btn').keypress(function(e) {
if(e.which == 13) {
alert("Enter pressed");
//Call your function here, or trigger the button click.
}
});
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;
This should do what you want:
$('body').keyup(function(e){
if(e.which == 13 && $('#yourText').is(':focus')){
console.log("do your thing");
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.