Jump to content

Creating Event Listeners


Wolverine68

Recommended Posts

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>



Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.