Aug
2009
jQuery - A Love Story
Posted in jQuery Cookbook, jQuery
Over the past year or so, I've been endeavoring to learn jQuery. I put it off for a long time. I've always been a fan of JavaScript, and have been used to writing it for as long as I can remember. I couldn't justify taking the time to learn a new way to do something that I already knew how to do.
But little by little and more and more I kept hearing people espouse the virtues of jQuery. I finally resigned myself to sit down and give it a fair shake.
At first, yes it was a little daunting. I think I was still subconsciously resisting this new way of doing things. But little by little I started to see the power of jQuery. It isn't that it's just a different way to do something that I already know how to do... but it's a quicker and easier and more powerful way.
Case in point. Hiding and showing page elements. "Old-School JavaScript" would have us do something like this:
if (document.getElementById('myDiv').style.display == "block") {
document.getElementById('myDiv').style.display = "none";
} else {
document.getElementById('myDiv').style.display = "block";
}
I used to think that was pretty straightforward.
Used to.
Here's how to accomplish the same thing the jQuery way:
$('#myDiv').toggle();
That's where it all began. My love affair with jQuery. jQuery, you had me at toggle(). I also took note of how easy selectors made it to access a particular element. I'd gotten pretty good over the years at typing out document.getElementById(), but that doesn't mean I'm going to miss doing it.
One of the ways I like to learn a new technology is by lurking on help forums or mailing lists. In this case, it was the amazing (but be warned... high traffic) Google jQuery mailing list. As I'd see relatively easy questions come through, I'd try and tackle them myself and post back the answer. This way I'm learning, and they're learning. On occasion, someone would point out a more efficient way of doing something that I had suggested, which was just a bonus, as I learned more.
I've saved a good number of the simple code snippets that I wrote, and plan on posting a series of "jQuery Cookbook" type entries utilizing them. Most are pretty straightforward, but I think it will be a good resource/reference for those who are new to jQuery, and help gently introduce them into this wonderful world.
I don't consider myself to be an expert in jQuery by any means, so I encourage those who might see a better way to accomplish a given task to participate as well.


Comments
There are no comments for this entry.
[Add Comment] [Subscribe to Comments]