homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

using two events together

5+ Year Member

Msg#: 3565729 posted 1:05 pm on Feb 4, 2008 (gmt 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)




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

Msg#: 3565729 posted 9:18 pm on Feb 5, 2008 (gmt 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
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.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved