Forum Moderators: open

Message Too Old, No Replies

List attached events & functions

         

Arno_Adams

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

10+ Year Member



Hi,

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

TIA, AA

Bernard Marx

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

WebmasterWorld Senior Member 10+ Year Member



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

Arno_Adams

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

10+ Year Member



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

Bernard Marx

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

WebmasterWorld Senior Member 10+ Year Member



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...]

Arno_Adams

10:12 am on Jul 27, 2007 (gmt 0)

10+ Year Member



That's it, problem solved!

Thanks a lot.

AA