Chomper Stomping
jQuery/JavaScript/CSS 3/HTML 5, Java/PHP/Python/ActionScript, Git, Chrome/Firefox Extensions, Wordpress/Game/iPhone App Development and other random techie tidbits I've collected



autobiographical

January 27, 2010

Removing and re-using hard-coded onclick JavaScript methods with jQuery

Let’s say you’ve been tasked with the ominous task of taking a currently “working” checkout process written in a hodge-podge of Prototype, jQuery and plain jane JavaScript and adding click methods to all input and a elements. Now, let’s also say you can’t modify the current HTML/JavaScript at all (mostly because it’s all working and you don’t want to break it and it’s already indecipherable code). Now, let’s also say that the click method you are attaching needs to be hands down, no matter what, the FIRST method that executes when you click on the element.

Sure you can make your JavaScript the first JavaScript that gets run on the page (so that the dynamic bindings run first, and therefor (hopefully) execute first). But what about the HARD CODED html click methods? This little code snippet will go through and dynamically remove (and “cache” (save for later)) all of the hard-coded onclick functions, and then bind your live functions to do your new critical must trigger first event, and then call the original onclick method on the element.

Yes, this code is scary. Yes, I cried a little after I wrote it while curled up in a ball under my desk. Yes, I plan to submit it to thedailywtf.com. But sometimes you just have to do what you have to do. If you’re in a similar situation, this might just help you out a little. Prepare to throw up in your mouth, hold on to your hats, and dive in:

To understand this a little more, you might like to throw a whole bunch of console.log() statements in there to see the state of each variable at each step along the way. It’s actually kind of fascinating that this code even works.

The toString() on the onClick on the attr call actually returns:
"function onclick(e){doThing();}

This is scary/cool/weird since the attribute it’s pulling is just
<input type="checkbox" onclick="doThing()" />

So I go through with the regex and strip off the “ onclick” leaving just an anonymous function: “function(e){doThing();}

I then assign the anonymous function to the variable “clickMethod” thus creating a brand new function on the page called clickMethod, which can be triggered or called anywhere I want and even passed an event object!!!

*BARF* XP

Someone please tell me there’s a better way… If so, why not hook me up on FIXEE???



About the Author

Christopher McCulloh
E-Commerce developer at Finish Line Co-Author of HTML, XHTML and CSS All-in-one Desk Reference for Dummies Graduated from IU with a Bachelors of Media Arts and Science and a Certificate in Applied Computer Science. Tech Editor for Building Facebook Applications for Dummies and Building Websites All-in-one for Dummies 2nd Edition. Creator and maintainer of the Status-bar Calculator Firefox Extension Three years professional experience in Java E-Commerce Development and four years professional experience with PHP for a combined total of seven years professional JavaScript/HTML/CSS experience




 
 

 
logo

dynode Batch Get Item

Working a lot with node.js, dynode and dynamoDB recently. Still trying to wrap my head around it all. Had a horrible time getting dynode.batchGetItem to work. Here is the error I was getting: { name: 'AmazonError', type: 'Valid...
by Christopher McCulloh
0

 
 
mysqlerror

WP phpBB Bridge: Warning: mysql_set_charset() expects parameter 2 to be resource, boolean given

Warning: mysql_set_charset() expects parameter 2 to be resource, boolean given in wp-content/plugins/wp-phpbb-bridge/inc/widgets/wpbb_topics_widget.php on line 149 This is an error caused by the fact that the WP phpBB Bridge pl...
by Christopher McCulloh
0

 
 
 

Events Calendar Pro Nav Formatting Messed up on Empty Calendar

The Events Calendar Pro (from http://tri.be/) has a few problems. If you are trying to figure out why a calendar with no events in that month has completely screwed up header navigation, just put this line of code inside of tab...
by Christopher McCulloh
5

 

 
warning

OH SHNIKES, WE’VE BEEN HAXORED!!!

Yes. It finally happened. After… 6 years? on the web I finally got hacked. Two domains affected: http://cmcculloh.com http://hallelujahbutton.com (this also of course affected all sub-domains of cmcculloh.com, such as blo...
by Christopher McCulloh
2

 
 
blue-xl

WordPress Settings API – Adding Options to Existing Page

Adding new options to an existing page in the dashboard in wordpress can be maddening. I’ve literally spent 15+ hours dealing with this horrible API at this point. To the point where I wrote two different wrappers for it....
by Christopher McCulloh
0

 




3 Comments


  1. Reedyseth

    Thank you very much Chris for this code, I looked every every on the net and you code did the job, specially this:

    jQuery.each(jQuery("div[onclick]"), function(){
    //create an "onclickmethod" attribute and assign it the FUNCTION DEFINITION of the onclick method (in order to cache it for later use)
    jQuery(this).attr("onclickmethod", jQuery(this).attr("onclick").toString());
    //remove the onclick function bind
    jQuery(this).removeAttr("onclick");
    });

    With this code I was able to remove de onclick attribute and then play with it’s value with jQuery.



Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>