My first foray into the world of jQuery

Well, I've finally gotten curious enough to give jQuery a try. Don't cry Adobe - your super powerful cfajaxproxy tag along with some of your widgets (which I don't know whether to hug or strangle on an hourly basis) have and will continue to serve me well. I finished my Certified ColdFusion Expert test earlier this month and now I'm on the prowl to learn something that will prove to be useful along with being exciting, challenging and new. It doesn't appear as though this jQuery phenomenon is going anywhere - so what do I have to lose!

So, without further delay, my first crack at jQuery involves a simple animation - a toggle that will show and hide a div when I click on a link. As a preliminary footnote, I will warn anyone else who is starting to learn jQuery that if you're not currently a practitioner of css and you want to learn jQuery then you'd better make friends with css quickly - or else!

The script below is simple enough. It first registers a ready event for the document. Pay close attention to the $("a") in there. For our example, "a" is a selector. From what I can tell, selectors are a big part of the foundation that jQuery is built upon. Can you see the css shining through yet?

At any rate, we could actually make this a named selector that corresponds to the id of an element but for now, any old link that we add to this page would actually call our function and toggle the div named stuff at a rate of 100 milliseconds.

 <html>
 <head>
 <link rel="stylesheet" type="text/css" href="css/main.css">
 <script type="text/javascript" src="jquery/jquery-1.3.2.min.js"></script>
 <script type="text/javascript">
     $(document).ready(function(){
         $("a").click(function(){
             $('#stuff').toggle(100);
             return false;
10          });
11      });
12  </script>
13  </head>
14  <body>
15      <a href="">Toggle Hide/Show of div 'Stuff'</a>
16      <br><br>
17      <div id="stuff" style="background-color:#FFFF00; font-size:14px; font-family:Tahoma, Verdana;">
18          Hello jQuery - I will unlock your power - even if it takes years!<br>
19          BTW, this is the content inside of a div named Stuff that jQuery is going to show and hide...
20      </div>    
21  </body>
22  </html>

This line shown above... $('#stuff').toggle(100); is where the rubber meets the road. We're basically using jQuery's toggle effect which uses the show method if an element is hidden or uses the hide method if the element is already displayed. One other quick note, we're using a link in this example to fire the toggle when the click fires on the link. I had to throw in the return false; to stop the link from executing it's default behavior.

Visually, that quick script will give us something that looks like this...

As expected, if you click on our link then jQuery will hide the div named 'stuff'.

To show it you just click on the link again and you're toggling the div.

So really that's about it. Easier to get started with then I had thought. I'm going to start pushing myself to use jQuery wherever I can within client projects and anything else that I'm working on so hopefully I will have some real world examples to publish in the coming days. I've always found the best way to learn something is to use it!

Related Blog Entries

TweetBacks
Comments
andy matthews's Gravatar You'll love learning jQuery. I make the case that jQuery is to Javascript, as ColdFusion is to Java.

If you're also interested in learning AIR, then you might be curious about some of the jQuery & AIR tutorials I've written over on my blog:
http://andymatthews.net/category/jQuery/

Good luck. jQuery is fantastic.
# Posted By andy matthews | 9/16/09 6:46 PM
Aspects's Gravatar that tight, if you are gonna post these often I'll attempt to learn jquery w/ and see what I can add to the mix

think I might anyway but it'd be interesting to swap info
# Posted By Aspects | 9/16/09 8:48 PM
Andy Sandefer's Gravatar @Andy
Thanks a lot for the encouragement - I'll be sure to check out your site!
# Posted By Andy Sandefer | 9/17/09 10:27 AM
Ben Nadel's Gravatar @Andy, yeah, you're gonna LOVE jQuery. It's just amazing. It truly opens up a whole new world of UI development.
# Posted By Ben Nadel | 9/17/09 11:14 AM
Jason Dean's Gravatar Yes! jQuery is teh awesome. You will definitely get addicted. jQuery has made me LOVE front-end development. It makes things SO MUCH easier. It also allowed me to get into AIR development without having to learn Flex first. (For the record, I am still learning Flex, but I did not have to wait to get started in AIR development).
# Posted By Jason Dean | 9/17/09 12:22 PM
Andy Sandefer's Gravatar @Everyone
Thanks a lot for the encouragement! I tried my hand at some more jQuery coolness just today (jQuery Day 2) and wrote a short blog entry about it...
http://www.cfpadawan.com/index.cfm/2009/9/17/Using...
# Posted By Andy Sandefer | 9/17/09 6:36 PM
BlogCFC was created by Raymond Camden. This blog is running version 5.9.5. Contact Blog Owner