i am trying to alert 1 on enter keypress can anyone help me to solve this? i am doing this inline because to work in rows and on every time enter key press send a row num with that request
<input type="text" id="tt" name="tt" onkeydown="javascript: if (keyCode == 13) alert(1)"/>
i am trying to alert 1 on enter keypress can anyone help me to solve this? i am doing this inline because to work in rows and on every time enter key press send a row num with that request
<input type="text" id="tt" name="tt" onkeydown="javascript: if (keyCode == 13) alert(1)"/>
Share
Improve this question
asked Mar 7, 2013 at 11:56
Umer FarooqUmer Farooq
842 silver badges9 bronze badges
2
-
You need to use the Event interface (first parameter or
window.event
). – Marcel Korpel Commented Mar 7, 2013 at 11:57 - This could be acplished without it being inline. – Jeff Shaver Commented Mar 7, 2013 at 12:07
3 Answers
Reset to default 9If you want to use inline js, just put event.keycode instead of keycode you can use:
<input type="text" id="tt" name="tt" onkeydown="javascript: if(event.keyCode == 13) alert(1);"/>
why inline?? when you can call event in jquery
try this
$('#tt').keydown(function(e){
if (e.keyCode == 13)
{
alert(1);
}
}
and if incase you need inline... you need to call event.keyCode
to get the keycode of key pressed
<input..name="tt" onkeydown="yourFuncName(event)"....>
function yourFuncName(e){
if (e.keyCode == 13)
{
alert(1);
}
}
<input type="text" id="tt" name="tt" onkeydown="myFunction(event)"/>
<script type="text/javascript">
function myFunction(e) {
if (e.which === 13 || e.charCode === 13 || e.keyCode === 13)
alert(1);
}
</script>
Please not that not all browsers support keyCode
. Some use which
and others use charCode
.