Posts Tagged ‘jQuery’

Way to throw down the gauntlet

Monday, March 1st, 2010

So, I’m innocently listening to the YayQuery podcast this morning, and some guy calls in questioning the use of the try/catch statement (no name). After he asks his question Rebecca Murphy makes the assertion that, “That was like our best yayQuery partyline question YET”. I was like, “aw man, she’s right…” but then Paul Irish, Adam J. Sontag and Alex Sexton (Who uses the exact same wordpress theme as me, so crap, now I have to change my theme again) all start back-peddling for her saying “No offense to Chris McCulloh” etc. Thanks guys. She’s right though, especially once she added the clarifying, “that was like, an in-depth question”. My questions were very short.

So, now I have to come up with a *good* question to ask. I’ve even got one brewing, but who knows if it will live up to Rebecca’s standards ;) . I shaved off the beard, so maybe I’ll even record it to video and send it in like I did the other ones…

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

Wednesday, January 27th, 2010

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???

Diagonal Accordion with jQuery

Friday, January 15th, 2010

diagonalAccordion is a jQuery plugin that allows for accordion functionality, but in a diagonal angle of your choice!

I forked Charles Marshall’s diagonal-accordion- plugin (on github) which allowed for a 45 degree angle accordion and hacked it to allow for any angle.

Use it like:

$("#someElement").diagonalaccordion({
acc_width:500,
acc_height:300,
bar_size:45,
speed:'slow',
accordion:'.accordion',
coverage:4,
diagAdjust: 3
});

Or even just:

$("#someElement").diagonalaccordion({
diagAdjust: 5
});

The only difference between using my plugin and his is the diagAdjust parameter. This parameter allows for incremental adjustments away from 45 degree angles. The bigger number you provide, the shallower the angle gets, until at number 11 it is effectively no angle. then after 11 it starts angling the other way.

Here’s the official demo for the original plugin. Like I said, mine works exactly the same way, it just has an extra param to use.