Skip to content Skip to sidebar Skip to footer

A Bookmarklet That Adds An Onclick Event To Every Element On The Page?

I want to make bookmarklet, and when you press it all the elements on the page gets an onclick event that calls a function with the element as a parameter or at least the elements

Solution 1:

click bubbles. So just bind to the <body> and check what the target of the event element is (you'll need to do a bit of event normalization to get around IE being different from everyone else.)

event.target is the actual DOM element, so you can filter on anything that you want to filter on at that point.

This has the advantage that it doesn't matter how many elements are on the page, you only need one event handler for all of them. (Which means performance will be better on pages with hundreds or thousands of elements.)

Solution 2:

javascript:void( document.body.onclick = function (event) { event = event || window.event; var element = event.target || event.srcElement; alert(element.textContent) } )     

Post a Comment for "A Bookmarklet That Adds An Onclick Event To Every Element On The Page?"