Reputation: 15
I have to perform some actions when a user presses enter on a selected <li>
. Currently I am performing on click event as:
liList[i].addEventListener("Click",getText,false);
I am calling the function getText
on click
event. Now I want to add keyboard enter event as well. How can I do this?
Upvotes: 0
Views: 74
Reputation: 73906
You can do this inside the getText
function:
function getText(e){
var e = e || window.event
var code = e.keyCode || e.which;
if (code === 13) { // enter key pressed
// your code
}
}
Upvotes: 0
Reputation: 5176
$(liList[i]).keypress(function(e) {
if (e.which != 13) return; // 13 is the enter key code
getText(e);
});
Upvotes: 0
Reputation: 337560
The code you have is native JS, however you've tagged your answer as jQuery, so I'll answer as such:
$(liList[i]).keypress(function(e) {
if (e.which == 13) { // 13 = enter key code
getText(e);
}
});
Upvotes: 3