Welcome to WebmasterWorld Guest from 54.146.217.179

Forum Moderators: open

Message Too Old, No Replies

using two events together

     
1:05 pm on Feb 4, 2008 (gmt 0)

New User

10+ Year Member

joined:Apr 21, 2005
posts:24
votes: 0


Hey Guys...
I have css box with two form buttons in it ok and cancel,
The javascript for cancel button is onclick='javascript:cancelalert(); I need this script to work even when I press the 'Esc' Key on the keyboard
How do i write in the script..
(In short button should work when 1. clicked and 2. on pressing the 'Esc' Keyboard key)

Thanks
Vicky...

9:18 pm on Feb 5, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member eelixduppy is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 12, 2005
posts:5966
votes: 0


Since both of these events are independent of each other, this is totally possible to accomplish. The first one you already have done, and that is with the use of the onClick event you have set up. The second is going to need to be done using the documents "onkeypress". It would look something like the following:

function keyHandler(e)
{
var pressedKey;
if (document.all) { e = window.event; }
if (document.layers) { pressedKey = e.which; }
if (document.all) { pressedKey = e.keyCode; }
if(pressedKey == 27) { //27 is the escape key
cancelalert();
}
}
document.onkeypress = keyHandler;

Not sure if this will work on mozilla, however. You can try if you'd like, but I believe this is an IE solution only. Try a nice google search to see what you can come up with in terms of cross browser scripting.