---
title: "why are use the attr function in JQuery? explain with example."  
description: "why are use the attr function in JQuery? explain with example."  
author: "Ethan Karla"  
published: 2021-07-28  
canonical: https://answers.mindstick.com/qa/93812/why-are-use-the-attr-function-in-jquery-explain-with-example  
category: "web application"  
tags: ["web application development", "web development"]  
reading_time: 1 minute  

---

# why are use the attr function in JQuery? explain with example.

why are use the attr [function in JQuery](https://www.mindstick.com/forum/157818/what-is-the-purpose-of-the-each-function-in-jquery-explain-with-an-example)? [explain](https://yourviews.mindstick.com/view/86025/content-created-for-humans-not-for-bots-explain-this-statement) with example.

## Answers

### Answer by Ravi Vishwakarma

Jquery attr() is used to set and get the attribute properties of a selected element, such as width, height, select, and others.

```
Syntax
$(selector).attr(attribute)        return attribute and value$(selector).attr(attribute,value)set an attribute and value $(selector).attr(attribute,function(index,currentvalue))set multiple attributes and values using function$(selector).attr({attribute:value, attribute:value,...})
```

```
Example
<!DOCTYPE html>
<html lang='en'>
<head>
  <meta charset='utf-8'>
  <title>scrollTop demo</title>
  <script src='https://code.jquery.com/jquery-1.10.2.js'></script>
<script>
    $(document).ready(function () {
        $('button').click(function () {
            $('img').attr({ width: 100}).show(1000);
        });
    });
</script>
</head>
<body>
<img src='https://source.unsplash.com/1600x900/?nature,water' width='500px' ><br>
<button>Set the width attribute of the image</button>
</body>
</html>  
```

Output:

![why are use the attr function in JQuery? explain with example.](https://answers.mindstick.com/questionanswer/53ccf519-5908-40f0-bd11-3446240f8c5e/images/e391080b-450a-4087-a248-ea9aa28bffeb.png)

\


---

Original Source: https://answers.mindstick.com/qa/93812/why-are-use-the-attr-function-in-jquery-explain-with-example

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
