Tuesday, September 21, 2010

Filter and Adding

A handy command to use when you want to eliminate elements that you have selected is filter. This method works very similar to what it sounds like. Lets look at an example:

$('#ravens p').filter(':not(:last)').hide();

In this simple example we are simply hiding all the ravens paragraph tags, but not the last one.

We can also add elements by using the add method. It works fairly similar to filter except in the opposite way. Let's view an example:

$('#cardinals p').add(':not(:first)').show();

This would allow for your selected cardinal paragraph tags to have elements added by showing them, yet not the first paragraph tag.

Of course you can combine the two methods add and filter, to really get a fine tuned selection of elements. This is a great technique to do a lot with a single line of jQuery code.

No comments:

Post a Comment