Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can I mimic pressing the enter button from within a <input>, using jQuery?
I want to simulate the enter button being pressed inside the textbox, and trigger this from jQuery.Please Help
Posted
Updated 12-Jun-18 0:56am

use keypress event in jquery

JavaScript
$( "#textboxid" ).keypress(function( event ) {
  if ( event.which == 13 ) {
     //code here
     //13 represents Enter key
  }
});


or you can use

JavaScript
$("#textboxid").live('keypress',function(event){
     if(event.which == 13){
         //code here
     }
 });


More info from here
 
Share this answer
 
v2
Comments
Naser Hassan 27-Oct-14 12:43pm    
this will work if user hit any key ....but i dont want user intervention ..i want to press enter key from code only
Dilan Shaminda 27-Oct-14 13:01pm    
It will not works if user press anykey..It prevents by the if condition.
Use "trigger" function of jQuery on page load or whenever needed to fire event:

$(document).ready(function () {
$("#YourControlID").trigger("TheEventNameYouWantToFire");
});

For more:
http://api.jquery.com/trigger/[^]
http://stackoverflow.com/questions/4247264/how-to-trigger-jquery-change-event-in-code[^]

To put cursor inside the text box when user hover the mouse on it:
$(this).ready(function (){
    $("#YourTextBoxId").hover( function (){
        $("#YourTextBoxId").trigger("focus");
    })
});


Thanks.
 
Share this answer
 
v3
Comments
Naser Hassan 27-Oct-14 12:34pm    
I want to hit Enter when the focus is on Textbox through code .how can i do ??
that trigger part i didn't get :(
Snesh Prajapati 27-Oct-14 12:54pm    
It seems you want to put cursor inside the text box when user hover the mouse over that text box...right ? if it is so I have added such code in answer.
Snesh Prajapati 27-Oct-14 12:58pm    
Let us know if the new code given.....solves your issue.
Naser Hassan 27-Oct-14 15:01pm    
user wont do any thing ...it is automate form fill up kind of thing ...i have one form which is submitted only when you hit "{ENTER}" if textbox has focus..
i am able to focus to textbox but not able to hit {"ENTER"} through code....
use keyup event in jquery

$("#id_of_textbox").keyup(function(event){
if(event.keyCode == 13){
$("#id_of_button").click();
}
});
document.getElementById("id_of_textbox")
.addEventListener("keyup", function(event) {
event.preventDefault();
if (event.keyCode == 13) {
document.getElementById("id_of_button").click();
}
});




You wrote:

 
Share this answer
 
v2
Comments
Richard Deeming 12-Jun-18 10:40am    
Asked and answered FOUR YEARS AGO.

Stick to answering recent questions.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900