Chapter 4. Manipulating Content and Attributes

jQuery is a thorough library, and it provides everything you can imagine for working with content from the DOM. In Chapter 2, you saw how jQuery made it really easy to fetch elements from the DOM via its fine-grained support for selecting and filtering selections. Then in Chapter 3, you saw how jQuery built on the W3C event model and made it less verbose while providing more methods that you can use in every browser today. This chapter continues the discussion of jQuery's API components with an in-depth look at the methods that jQuery makes available for manipulating content and attributes. No longer do you have to worry about whether a browser supports the innerText or textContent properties, or the outerHTML property, or what the standard DOM method of removing an element from a document is. jQuery paves right over these frighteningly verbose and sometimes fragmented methods with a rock-solid API that just works.

In this chapter, I cover how you can shuffle 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.

I also cover how you manipulate attributes using jQuery, another area that fringe use cases in some browsers (cough, IE) can make difficult.

Or maybe you've had occasion to want to save custom data with an element, without having to mess around with creating custom attributes or hidden elements. jQuery provides ...

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.