views:

157

answers:

7

hey all - looking for a way to list all onclick's on a page,or elements that contain an onclick. Please help!!

+1  A: 

Edited to answer further questions...


I'm not sure exactly what you're asking, but I suspect you're looking for a way to see if there has been code attached to an element's onclick event? If so, try this:

var allElements = document.getElementsByTagName('*');
for ( var i = 0; i<allElements.length; i++ ) {
    if ( allElements[i].className !== 'theClassNameYoureLookingFor' ) {
        continue;
    }
    if ( typeof allElements[i].onclick === 'function' ) {
        // do something with the element here
        console.log( allElements[i] );
    }
}

If you'd like some information about the actual function attached to the onclick event, you'll need to make use of Firebug or something similar to output the function and examine it as it will have been possibly generated at runtime (ie the function declaration might not just be sitting on the page where it could theoretically be accessed easily). Try using the code provided above, but instead of

console.log( allElements[i] );

do

console.log( allElements[i].onclick );

Now, in firebug, you can mouse over the functions it prints and see their code.

Josh the Goods
well i have a page with a bunch of divs that share the same class name. those divs also have onckicks attached to them. I want to extract the data that is inside each onclick. I believe the code posted above would list all teh elements on page... so, i need to get all elements with the same class name, and then get the parameters that are in each onclick
toli
Post edited to provide more information. Be sure to read bobince's answer as well as he makes some good observations.
Josh the Goods
A: 

If you use jQuery >= 1.4, you can get an Array of all elements with onclick attribute by writing the oneliner:

$('body *').toArray().filter(function(el) { return $(el).attr('onclick') });
matley
+1  A: 

i need to get ... the parameters that are in each onclick

You can read the string value of the onclick attribute by using:

var onclickstr= allElements[i].getAttributeNode('onclick').value;

inside Josh's code (this is a slightly roundabout method, required for IE).

Of course it would only work for event handlers attached from inline event handler attributes, not assigned to the onclick property from JavaScript or added via addEventListener or attachEvent.

Trying to prise values out of a JavaScript code string is an ugly and unreliable hack that you shouldn't resort to unless there's really no other option. If the page is something you're generating yourself, you should instead use unobtrusive-scripting techniques. Omit inline event handler attributes like onclick completely (today they're considered bad practice) and attach handlers from JavaScript itself, using parameters stored in easier-to-access places like classname and other ‘spare’ attributes where necessary.

bobince
A: 

This is fairly easy if the traditional/old school method of event handler attachment is used (the following code is untested).

var getEventHandlers = function(nodes, eventType) {
    var handlers = [], h;

    for ( var i=0, l=nodes.length; i<l; i++ ) {
        h = nodes[i].getAttribute(eventType);
        if ( typeof h === "function" ) {
            handlers.push(h);
        }
    }

    return handlers;
};

Use:

console.log(getEventHandlers($("*"), "onclick"));
console.log(getEventHandlers($(".some-class"), "onclick"));
Justin Johnson
A: 

Thank You ALL !!!!!

toli
You might want to accept one of them as an accepted answer. ;)
Gert G
A: 

would you guys know this... basically, what im doing in loading an html page into adobe air application. I have access to the document of the loaded page and, for example, can get all images in it by calling document.images;
I'm assuming that for these functions you provide, the scope would be the dom of the page js resides on. Can I pass a reference to my document, so these functions would work for the particular html page i loaded into my air app? I can call javascript functions from air...

toli
figured it all out... basically had to do everything inside of air, thus the scope was no problem
toli
A: 

For a succinct jquery selector, you could use $('[onclick]') however that would only get elements wit the onclick attribute set.

Josiah