...

View Full Version : Identify the element generated the event.



Vijay Venkat
01-19-2006, 02:19 PM
Hi,
Is there a way to identify the element which called the javascript function?
If it is a event, then i can identify from the event object
If it is like <a href="javascript:test()" /> . Is it possible to identify that it was this anchor element which called the function test. I understand, i could have added a id attribute with some value and could have passed it via test(). There are n number of call in different pages similar to what i have identified above. Hence it will be difficult to find it.

Thanks,
Vijay

A1ien51
01-19-2006, 05:28 PM
This may help:
http://www.quirksmode.org/js/events_properties.html

Eric

SpirtOfGrandeur
01-19-2006, 05:36 PM
You will also need to change

<a href="javascript:test()" />
to

<a href="javascript:test(event)" />

To make it work in all browsers.

Kor
01-20-2006, 12:14 PM
I am thinking of another aproach, using getAttribute method and taking care about the IE/Moz difference:


<script type="text/javascript">
var myEl=[];
function searchF(){
var e = document.getElementsByTagName('*');
for(var i=0;i<e.length;i++){
if(e[i].getAttribute('onclick')||e[i].getAttribute('onclick')!=null){// IE/Moz
var re= e[i].getAttribute('onclick').toString().match(/foo()/g);
re!=null?myEl[myEl.length]=e[i]:null;
}
}
}
onload = searchF;
</script>

Now you have a collection of elements (myEl[]) which trigger onclick the function foo()

glenngv
01-23-2006, 08:41 AM
Kor, you still won't know which particular link was clicked.



There are n number of call in different pages similar to what i have identified above. Hence it will be difficult to find it. It's not that difficult. There are many text editors that can "Find and Replace in Files".
Seach this string:

href="javascript:test()"
and replace it with:

href="#" onclick="theFunc(this); return false;"
And you're done. The theFunc() function now has the reference to the link. No more complicated script.

Kor
01-23-2006, 09:17 AM
Kor, you still won't know which particular link was clicked.

It depends on. My guess was that Vijay Venkat needed to know which are the elements as a collection, not as a certain element, at the precise moment of launching the event...

Even so, there might be a way to add dynamically another function to be triggered onclick... I have studied this for some days, not a final result till now... But maybe this is another thread subject....



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum