Welcome to WebmasterWorld Guest from 54.162.226.212

Forum Moderators: open

Message Too Old, No Replies

using two events together

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

10+ Year Member



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)

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



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.

 

Featured Threads

Hot Threads This Week

Hot Threads This Month