O'Reilly logo

jQuery Pocket Reference by David Flanagan

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Element Methods

The methods described here query and set the HTML attributes and CSS style properties of elements. Setter callback functions with an argument named current are passed the current value of whatever it is they are computing a new value for; see Chapter 2.

addClass(names)addClass(f(idx,current):names)

Add the specified CSS class name (or names) to the class attribute of each selected element. Or, invoke f as a method of each element to compute the class name or names to add.

attr(name):valueattr(name, value)attr(name, f(idx,current):value)attr(obj)

With one string argument, return the value of the named attribute for the first selected element. With two arguments, set the named attribute of all selected elements to the specified value, or invoke f as a method of each element to compute a value. With a single object argument, use property names as attribute names, and property values as attribute values or attribute computing functions.

css(name):valuecss(name, value)css(name, f(idx,current):value)css(obj)

Like attr(), but query or set CSS style attributes instead of HTML attributes.

data():objdata(key):valuedata(key, value)data(obj)

With no arguments, return the data object for the first selected element. With one string argument, return the value of the named property of that data object. With two arguments, set the named property of the data object of all selected elements to the specified value. With one object argument, replace the data object of all selected elements

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required