Appendix E. Manipulating Attributes and Data Caching

Method/Property

Description

Return Value

attr()

attr(name)

Returns the attribute value for the specified attribute from the first element present in a selection. If no element is present, the method returns "undefined."

Object

attr(properties)

Allows you to set attributes via the specification of key, value pairs. For example:

attr({
     id: 'someIDName',
     href: '/example.html',
     title: 'Some tooltip text.'
});

jQuery

attr(key, value)

Allows you to specify an attribute by providing the name of the attribute in the key argument and its value in the value argument.

jQuery

attr(key, function)

Sets an attribute's value depending on the return value of the callback function that you specify. The callback function is executed within the context of each selected element, where each selected element can be accessed within the function via this.

jQuery

removeAttr(name)

Removes the specified attribute from the element(s).

jQuery

Class Names

addClass(class)

Adds the specified class name to each selected element. Elements can have one or more class names.

jQuery

hasClass(class)

Returns true if the specified class name is present on at least one of the selected elements.

Boolean

removeClass(class)

Removes the specified class name from each selected element.

jQuery

toggleClass(class)

Adds the specified class name if it is not present, and removes the specified class name if it is present.

jQuery

HTML

html()

Returns the HTML contents, or innerHTML, of the first element of the selection. ...

Get Beginning JavaScript® and CSS Development with jQuery 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.