Getting and Setting DOM Element Attributes and Properties

DOM objects provide direct access to the DOM object attributes as well as the DOM element properties of the HTML elements they represent. This really is not reasonable in jQuery, because jQuery objects often represent multiple elements with varying attributes. For that reason, jQuery provides the .attr() and .prop() methods to get and set the attributes and properties of these elements.

The .attr(attribute, [value]) method allows you to specify an attribute name only to get the current value as well as an optional value to set the current value. For example, the following code gets the src value for a specific image element with id="bannerImg":

var state ...

Get jQuery and JavaScript Phrasebook 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.