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

Get jQuery Pocket Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.