Thursday 24 June 2010

Using jQuery pseudo selectors

jQuery offers a powerful set of tools for matching a set of elements in a document.

If you wish to use any of the meta-characters (#;&,.+*~':"!^$[]()=>|/ ) as a literal part of a name, you must escape the character with two backslashes: \\. For example, if you have an an input with name="names[]", you can use the selector $("input[name=names\\[\\]]").

Attribute Ends With Selector [name$=value]

jQuery('[attribute$=value]')

where

attribute = An attribute name.
value = An attribute value. Quotes are optional.

Description: Selects elements that have the specified attribute with a value ending exactly with a given string.

Example

<input name="newsletter" />
<input name="milkman" />
<input name="jobletter" />
<script>$("input[name$='letter']").val("a letter");</script>

:first Selector

Description: Selects the first matched element.

The :first pseudo-class is equivalent to :eq(0). It could also be written as :lt(1). While this matches only a single element, :first-child can match more than one: One for each parent.

Example
<table>
<tr>Row 1
<tr>Row 2
<tr>Row 3
</table>
<script>$("tr:first").css("font-style", "italic");</script>


http://api.jquery.com/category/selectors/

No comments:

Post a Comment