Fri Mar 19 10:17:47 CET 2010


The following is in the Firebug console.  The prototypical use of
jQuery is to iterate over a collection.

>>> $("a").map(function(indx, el) {return this.getAttribute("href");})

The $ here is an alias for the jQuery constructor.  $("a") constructs
a query which abstracts a collection of all <a/> nodes in the
document.  Query objects supports the `each' method which applies each
object in the query to a function.  Also supported is the `map' method
which additionally collects function results in a list.

That particular example is simpler to do as:

>>> $("a").attr("href")

which returns the attribute for the first element in the selection.

Most of the API seems to be imperative; methods modify the state of
the document.

To set the text of a collection of nodes, use:

>>> $("a").text("SIPRI")
[a www.sipri.org]

The syntax that's used in the string passed to to the $() constructor
has elements of XPath.

[1] http://en.wikipedia.org/wiki/Jquery