Manipulating Content and Attributes
Richard York · 2015
This chapter discusses jQuery's API components with an in-depth look at the methods that jQuery makes available for manipulating content and attributes. It shows how one can shuffle Document Object Model (DOM) content around, doing things like replacing one element with another, inserting new text or HTML, appending or prepending content, cloning content, and getting rid of content. Setting multiple attributes is done by providing a JavaScript Object Literal to the attr() method. The chapters discusses addClass(), hasClass(), removeClass(), and toggleClass() methods that jQuery uses to manipulate class names. It explains how content manipulation in jQuery works. The simplest methods that jQuery provides for content manipulation are the html() and text() methods. The methods that jQuery provides that enable one to wrap content are wrap(), wrapAll(), and wrapInner(). The chapter discusses two methods for replacing elements, jQuery's replaceWith() and replaceAll(). jQuery provides a method called clone() for cloning (copying) content.