I would like to run a js function on pressing Enter button.
Everything works great in IE, Chrome and Opera, but I get an error (explained below) in Safari.
I need a pure Javascript solution, not jQuery.
This is the function:
function pressSearch(e) {
if (typeof e == 'undefined' && window.event) {
e = window.event;
}
var charCode = (e.which) ? e.which :
((e.charCode) ? e.charCode :
((e.keyCode) ? e.keyCode : 0));
if (charCode == 13) {
document.getElementById('enterSearch').click();
}
}
This is the HTML:
<input type="text" id="searchKey" onkeypress="pressSearch(event)">
<div id="enterSearch">Search</div> // This is the "button"
I get this error:
// Safari
TypeError: 'undefined' is not a function
(evaluating 'getElementById('enterSearch').click()')
What am I doing wrong?
EDIT: I've fixed the Mozilla Firefox error.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…