Welcome to WebmasterWorld Guest from 34.238.189.171

Forum Moderators: open

Message Too Old, No Replies

List attached events & functions

     
7:44 am on Jul 27, 2007 (gmt 0)

Junior Member

10+ Year Member

joined:Dec 7, 2004
posts:130
votes: 0


Hi,

how can I loop through the DOM of a page and list all the elements with events and associated functions?

TIA, AA

9:21 am on July 27, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 15, 2004
posts:2047
votes: 0


I won't be presenting full code here..

1) Get a nodeList of all elements: document.getElementsByTagName("*");

2) Iterate over this list. With each element:

- Enumerate its properties, of which there will be many by default


var regOn = /^on/;
for(var p in elm){
if(regOn.test(p) && elm[p]){
do something with elm[p];
}
}

It's assumed that all event handlers begin with "on". All of these will appear, whether defined or not, so we test to see if they have been.

Problems (that I can think of right now)

1) Unfortunately, I'm pretty sure that event handlers assigned using addEventListener/attachEvent don't enumerate.

2) Need to enumerate the global object (window) separately

9:29 am on July 27, 2007 (gmt 0)

Junior Member

10+ Year Member

joined:Dec 7, 2004
posts:130
votes: 0


Hi Bernhard,

Thanks for the quick reply. I got that far i the mean time.
My problem is that via onload I attach events to certain form elements. These events override events & functions
attached to elements like this:


<select name="foo" id="foo" onchange=doStuff(this.value)">

But on some pages I need both. Any ideas?

TIA, AA

9:40 am on July 27, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 15, 2004
posts:2047
votes: 0


Yes. That is what addEventListener (standards) / attachEvent (IE) ar for. They are "non-exclusive". Many people use an "addEvent" function to bring these 2 together. For instance: [onlinetools.org...]
10:12 am on July 27, 2007 (gmt 0)

Junior Member

10+ Year Member

joined:Dec 7, 2004
posts:130
votes: 0


That's it, problem solved!

Thanks a lot.

AA