Sunday, July 31, 2011

Get keyCode on keypress in javascript


Add this javascript code:



<script>

function getKey(e)

{

//for internet fix

var k = (window.event) ? event.keyCode : e.keyCode;

alert(k);

}

</script>





html code:



<input type="text" onkeypress="getKey(event)" />

2 comments:

Daivagna Nanavati said...

You can also do it by using srcElement to distinguish between browsers...i.e (e.srcElement)? foo: bar; but you need to pass 'this' as an argument to the function instead of event...anyway theres always new ways..so just shared...

Vihang Shah said...

Ya, It might also be a way to handle this. I haven't tried yet.
Anyways, Thanks for the sharing..

@VHNG

Find a cool free stuff everyday

Giveaway of the Day

Hiren Bharadwa's Posts

DotNetJalps