PDA

View Full Version : Javascript: Key Detection



Sammo
March 15th, 2006, 02:22 PM
I have a function that is called on the onkeyup event for a textarea:

lArea.onkeyup = updateLivePreview;

in my updateLivePreview function I need to check if the key that was released was infact the Enter button, if so do x else do y. How would I acheive this?

rufusW
March 16th, 2006, 06:41 AM
Hi Sammo

Try this

function updateLivePreview(e)
{
var code;
if (!e) var e = window.event;
if (e.keyCode) code = e.keyCode;
else if (e.which) code = e.which;
if (code == 13){
alert("Enter is pressed");
}
}


hth