How to use jQuery to manipulate the DOM attributes

Web developer can manipulate the DOM, by modifying object of the page. He/she can add or remove the element; add, modify, or remove an attribute; and so on.

Web developer can use the attr method to add or modify the attribute. If the attribute exists, the method doesn’t write it again, but modifies the existing one. For example the code:


retrieves all check boxes and, for each of them, invokes the attr method which adds an attribute to the DOM element using the first parameter as the name and second as the value. The result is that all check boxes will have the following HTML:

<input type=”checkbox”, checked=”checked” />

Web developer can unselect all check boxes, by removing the checked attribute. He/she can do this by using the removeAttr method: