Wednesday, August 25, 2010

More selecting in jQuery

In our last post we touched on selectors in jQuery, however we just started scratching the surface. We can select multiple elements in a jQuery statement like the example below:

$('div, h3, p')

This selects every div element, h3 heading, and paragraph tag in the DOM (Document Object Model). This is the same as the selectors we have looked at before except that we are separating them with a comma to select more than one element.

Filters

One of the more subtle tricks to use when using a selector is to use a filter. A filter simply removes certain items by using a colon : followed by the filter name. An example of a filter can be found below:

$('#someId tbody tr:first')

This selects the first table row in the table body with id someId. To illustrate a similar example:

$('#someId tbody tr:last')

This (as you might guess) selects the last table row in the table body with id someId.

Other filters that we can use are :even, :odd, :eq(), and several others....

No comments:

Post a Comment