Manipulating attributes and properties
Throughout the first four chapters of this book, we have been using the .addClass()
and .removeClass()
methods to demonstrate how we can change the appearance of elements on a page. Although we discussed these methods informally in terms of manipulating the class
attribute, jQuery actually modifies a DOM property called className
. The .addClass()
method creates or adds to the property, while .removeClass()
deletes or shortens it. Add to these the .toggleClass()
method, which alternates between adding and removing class names, and we have an efficient and robust way of handling classes. These methods are particularly helpful in that they avoid adding a class if it already exists on an element (so we don't end up with <div class="first first">
, for example), and correctly handle cases where multiple classes are applied to a single element, such as <div class="first second">
.
Non-class attributes
We may need to access or change several other...