Click here to Skip to main content
Licence CPOL
First Posted 17 Jul 2006
Views 16,715
Downloads 66
Bookmarked 5 times

Identify the DOM element for a DOM event

By REA_ANDREW | 17 Jul 2006
Enabling you to determine the target element which has fired a DOM event.
1 vote, 33.3%
1

2

3
1 vote, 33.3%
4
1 vote, 33.3%
5
2.17/5 - 3 votes
μ 2.17, σa 3.64 [?]

Sample Image - DomEventTargetDetection.jpg

Introduction

This script will give you an idea of how to identify which elements on your page fire a specific DOM event. For the purposes of this article, I will use the onmouseover event and display the element which fires it.

Capturing the window event

Before I show you a working example, I will quickly discuss how to identify a window event, which I have tested in IE and FF. I will comment under each to briefly explain.

document.onmouseover = function(e){
   var e = e || window.event;
   //notice that when assigning variables you can use 
   //the OR to differentiate between the methods each broswer accepts

   var target = e.target || e.srcElement;
   //now we have established the target which fired the event
}

Preparing for the working example

In the example I am going to give, I have used some more methods which I feel I should explain prior to writing the code.

In my example, I will go through all the elements in the document which have an HTML tag of div. What I will then do is write the id of the div element to a TextArea which I have named RESULTS. To achieve this, I must create an iteration:

for(i=0;i<document.getElementsByTagName('DIV').length;i++)
{
    // We will execute the code here
}

Inside this iteration, we need to reference the current div element which the iteration is up to. Below I will repeat the above code, but this time, I will say, "alert" the div element id.

for(i=0;i<document.getElementsByTagName('DIV').length;i++)
{
    alert(document.getElementsByTagName('DIV')[i].id);
}

The last part of the code I would like to briefly show is how I achieved the scroll behaviour of the RESULTS TextAarea. You will notice that it always scrolls down, upon extra content being added. I achieve this by saying the scrollTop is equal to the scrollHeight, thus keeping it at the bottom.

document.getElementById("RESULTS").scrollTop = 
         document.getElementById("RESULTS").scrollHeight

Bringing it all together

Basically, you will now see the following behaviour. You mouseover any of the eight div elements, and the id of that element is written into the text area.

window.onload=function()
{
    for(i=0;i<document.getElementsByTagName('DIV').length;i++)
    {
        document.getElementsByTagName('Div')[i].onmouseover = function(e)
        {
            var e = e || window.event;
            var target = e.target || e.srcElement;
            document.getElementById("Results").value += 
               "onmouseover fired for :" + target.id +"\n";
            document.getElementById("Results").scrollTop = 
               document.getElementById("Results").scrollHeight;
        }
    }
}

You will notice that I used window.onload instead of using the onload event in the body tag.

Taking it further

We can take this even further by:

  1. Creating a function to handle each event.
  2. Instruct the function to write out what event it was, by referencing the event type.

The function will be similar to what you see in the onmouseover function event code above, but this time, we can use this new function for any event, and you will see all of them firing, in the way that it will write out what is happening.

window.onload=function()
{
    for(i=0;i<document.getElementsByTagName('DIV').length;i++)
    {
        document.getElementsByTagName('Div')[i].onmouseover = function(e){WriteId(e);}
        document.getElementsByTagName('Div')[i].onmousedown = function(e){WriteId(e);}
        document.getElementsByTagName('Div')[i].onmouseout = function(e){WriteId(e);}
        document.getElementsByTagName('Div')[i].onclick = function(e){WriteId(e);}
        document.getElementsByTagName('Div')[i].onmouseup = function(e){WriteId(e);}
    }
}

function WriteId(e)
{
    var e = e || window.event;
    var target = e.target || e.srcElement;
    document.getElementById("Results").value += e.type +" fired for:"+ target.id +"\n";
    document.getElementById("Results").scrollTop = 
       document.getElementById("Results").scrollHeight;
}

This should just give you a basis, showing you when events fire, and each time they do, it will write out the event for you in the TextArea.

HTML elements which I have used for this example

As you will see in the source code for this, I have used the following HTML elements in the body of the page for this example.

<div id="item1">item1</div>
<div id="item2">item2</div>
<div id="item3">item3</div>
<div id="item4">item4</div>
<div id="item5">item5</div>
<div id="item6">item6</div>
<div id="item7">item7</div>
<div id="item8">item8</div>
<textarea id="Results" cols="40" rows="10"></textarea>

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

REA_ANDREW



United Kingdom United Kingdom

Member


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralNice for me Pinmembersaeioul7:03 21 Aug '07  
GeneralRe: Nice for me PinmemberREA_ANDREW7:07 21 Aug '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120210.1 | Last Updated 17 Jul 2006
Article Copyright 2006 by REA_ANDREW
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid