Chapter 4. Working with properties, attributes, and data

 

This chapter covers

  • Getting and setting element attributes
  • Working with element properties
  • Storing custom data on elements

Everyone who has approached software development for the first time has learned a very important lesson: even huge and complex software consists of a mix of elementary instructions. Summing numbers, counting items, and iterating over elements are just a few examples of these basic operations. In the same way, you can create a nice visual with jQuery by manipulating attributes, properties, classes, styles, and so on.

You can manipulate attributes and properties of elements with JavaScript’s native functions, but some tasks aren’t as easy as you’d like them to be. In addition, using those functions leaves you with the burden of dealing with browsers’ incompatibilities. jQuery provides a full set of methods to easily work with attributes and properties, solving all the compatibility issues for you.

Another key concept when working with DOM elements and the creation of effects is the possibility of storing custom data on the elements you’re operating upon. jQuery allows you to save the state an element is in at a given time. This feature is crucial for creating plugins, as you’ll discover in part 3 of this book.

This chapter focuses on the many methods jQuery offers for working with attributes, properties, and data.

4.1. Defining element properties and attributes

4.2. Working with attributes

4.3. Manipulating element properties

4.4. Storing custom data on elements

4.5. Summary

sitemap