Appendix E

Manipulating Content, Attributes, and Custom Data

Method/Property Description Return Value
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. String,Undefined
attr(object) Allows you to set attributes via the specification of key, value pairs. For example:attr({ id : 'idName', href : '/example.html', title : '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(className) Adds the specified class name to each selected element. Elements can have one or more class names. jQuery
addClass(function()) Adds one or more space separated class names returned from a callback function. jQuery
hasClass(className) Returns true if the specified class name is present on at least one of the selected elements. Boolean
removeClass([className]) Removes the specified class name from each selected element. If multiple class names are provided, ...

Get Web Development with jQuery now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.