Wolverine68 Posted February 22, 2012 Share Posted February 22, 2012 Trying to create a simple Event Listener such that was a key is press the element of the id is highlighted. I'm using IE, so I used "attachEvent" instead of addEventListener. Not getting any response when any of the keys are pressed. http://goken68.brinkster.net/EventListeners.html <html> <body> <head> <script type="text/javascript" language="javascript"> var ev = document.getElementById("north") ev.attachEvent('onkeydown',highlight,false); ev.attachEvent('onkeyup',highlight,false); </script> </head> <div id="teams"> <h1>NFL Teams</h1> <h2 id="north">NFC North</h2> <p>Chicago Bears</p> <p>Green Bay Packers</p> <p>Minnesota Vikings</p> <p>Detroit Lions</p> <h2>NFC South</h2> <p>New Orleans Saints</p> <p>Atlanta Falcons</p> <p>Carolina Panthers</p> <p>Tampa Bay Buccannears</p> <h2>NFC East</h2> <p>Dallas Cowboys</p> <p>Washington Redskins</p> <p>Philadelphia Eagles</p> <p>NY Giants</p> <h2>NFC West</h2> <p>San Francisco 49ers</p> <p>Arizona Cardinals</p> <p>Seattle Seahawks</p> <p>St.Louis Rams</p> </div> Quote Link to comment https://forums.phpfreaks.com/topic/257563-creating-event-listeners/ Share on other sites More sharing options...
joe92 Posted February 22, 2012 Share Posted February 22, 2012 The key would have to be pressed within in the element you are attaching the event listener too. If you wish for something to happen when you press a key anywhere on the page, then add the event listener to the entire window. Keeping it as a keypress though, change your h2 tag to this: <h2 id="north" contenteditable="true">NFC North</h2> This will allow you to edit the contents of the h2 tag as if it were an input type. This means that when you focus in the h2 tag and then press a key, the event listener will do what you want it too. Otherwise, change the event listener to onmousedown and witness it happen when you click the tag. Hope this helps you, Joe Quote Link to comment https://forums.phpfreaks.com/topic/257563-creating-event-listeners/#findComment-1320138 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.