Blog Stats
  • Posts - 13
  • Articles - 0
  • Comments - 5
  • Trackbacks - 0

 

Some of My Favorite jQuery Things

This example shows two of my favorite things about jQuery:  chainable methods and the find method.  The find method is the easiest way to find decendent objects.  There are many other ways, but when you to perform work on the parent and the child, chaining the work keeps the code tight and reduces the number of object that must be created

Let's say you have something like this:

<div id='containerForList'>
    <span id='listLabel>Description of List Goes Here</span>
    <ul><!-- items go here -->
         

   </ul>
</div>

And you need to perform several tasks.  Display the Container and Add a new Item to the UL. 

There are several ways to get to it.  You can separate this into two calls, create an object for the div and then create an object for the ul and perform the tasks on those seperately.  Another approach is to chain these actions together.

$('#containerForList').show().find("ul").append("<li>A new Item to the list</li>");

 

Of course like all examples, they are always too contrived, but you get the idea.


Feedback

No comments posted yet.


Post a comment





 

 

 

 

Copyright © John Teague