What is the jQuery attr() method?

In jQuery, the attr() method is used to set or return the value of an element. To get the individual value for each element, a looping construct, ​such as jQuery’s each() method ​or map() method, is used.

Syntax

svg viewer

Code

The following codes explain how the value is set and returned using the attr() method.

The code is tested on the following image:

Setting a value

The following code decreases the image’s width from 300 to 200 pixels. Click on the button labeled Click to check the effect.

Returning a value

The following code returns the image’s width when the button labeled Click​ is clicked.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved