Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
158
rated 0 times [  164] [ 6]  / answers: 1 / hits: 115146  / 12 Years ago, fri, july 6, 2012, 12:00:00

I only have 1 input field and I want to register onChange and onKeyPress event to detect when the users finish their input or press the Enter key. I need to use javascript only. Thanks for the help.



I have:



var load = function (){
//I want to trigger this function when user hit Enter key.
}

document.getElementById('co').onchange=load; //works great
document.getElementById('co').onKeyPress=load;
//not sure how to detect when user press Enter


html



//no form just a single input field
<input type='text' id='co'>

More From » javascript

 Answers
79
document.getElementById('foo').onkeypress = function(e){
if (!e) e = window.event;
var keyCode = e.code || e.key;
if (keyCode == 'Enter'){
// Enter pressed
return false;
}
}

DEMO


[#84416] Thursday, July 5, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
leog

Total Points: 225
Total Questions: 113
Total Answers: 118

Location: Oman
Member since Wed, Apr 12, 2023
1 Year ago
;