spacer
Yehuda Shiran October 10, 2001
Detecting An Event within A Page
Tips: October 2001

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
Google Chrome Playing Catch-Up on Extensions
Open Solutions Alliance Gets New Leadership
Red Hat Spacewalk Expands Linux Management
Usually, you attach an event handler to a specific element on the page, such as an image, a text box, or a popup window. Sometimes, though, you want to attach an event handler to a group of elements. Instead of duplicating the attachment for all members of the group, you can attach the event handler to the containing page, and figure out during the event firing which element the event was fired on. When you want to catch an event within a page, you can use the attachEvent() method. The attachEvent() syntax is as follows:

  object.attachEvent(event, eventHandler);
If there is an additional event handler defined for the object the event fired on, it will be run before the function eventHandler() is called. If you attach several event handlers to the same page, they will be called in a random order, immediately after the object's event handler is run, if at all defined.

Let's take an example. In the page's init() function you can ask that the function mouseDownHandler() be called when the onmousedown event fires:

  document.attachEvent("onmousedown", mouseDownHandler);
And you can determine the object it fired on by going:

  objectClicked = event.srcElement.id;

People who read this tip also read these tips:

Look for similar tips by subject:

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

webref The latest from WebReference.com Browse >
Administering MySQL Databases on the Web Using PHP · Popular JavaScript Framework Libraries: An Overview - Part 3 · Accessing Your MySQL Database from the Web with PHP
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
So what is an Oracle Nested Table? · E-Discovery Architectures 101 · eBay Embraces Big Sellers, Cyber Monday Trends