jQuery
New Wave Javascript


jQuery includes some expressions that aren't available in either CSS or XPath, but were found to be very handy, so were included.

The following expressions' syntax is based upon the various CSS selectors, of similar names.

Custom Selectors

  • :even Selects every other (even) element from the matched element set.
  • :odd Selects every other (odd) element from the matched element set.
  • :eq(0) and :nth(0) Selects the Nth element from the matched element set
  • :gt(4) Selects all matched elements whose index is greater than N.
  • :lt(4) Selects all matched elements whose index is less than N.
  • :first Equivalent to :eq(0)
  • :last Selects the last matched element.
  • :parent Selects all elements which have child elements (including text).
  • :contains('test') Selects all elements which contain the specified text.
  • :visible Selects all visible elements (this includes items that have a display of block or inline, a visibility of visible, and aren't form elements of type hidden)
  • :hidden Selects all hidden elements (this includes items that have a display of none, or a visibility of hidden, or are form elements of type hidden)

Some examples:

$("p:first").css("fontWeight","bold");
$("div:hidden").show();
$("div:contains('test')").hide();

Custom Elements

One problem, as seen by jQuery, is that the 'input' element selects such a hodge-podge group of form elements - either it should select all, or none. To rememedy this, there is a new assortment of input elements to work with:

The input selects all form elements, including selects and textareas. The old CSS selector of 'input,select,textarea' is now equivalent to the jQuery 'input'.

To select individual input element types, you can use the shorthand: text, radio, checkbox, hidden, button, submit, image, password, reset, file, select, and textarea. For example

$("radio").hide();
$("text,textarea").set("value","hello");