Open In App

jQuery attr() Method

The attr() method in jQuery is used to set or return the attributes and values of the selected elements. 

Syntax:



$(selector).attr(attribute)
$(selector).attr(attribute, value)
$(selector).attr(attribute, function(index, currentvalue))
$(selector).attr({attribute:value, attribute:value, ...})

Parameter

Example 1: In this example, the image will expand when the button is clicked.






<!DOCTYPE html>
<html>
 
<head>
    <title>jQuery attr() Method
    </title>
    <script src=
    </script>
</head>
 
<body>
    <center>
        <h1 style="color:green;">
            GeeksForGeeks
          </h1>
        <h2> jQuery attr() Method</h2>
        <h3 style="color:lightgreen;"></h3>
        <img src=
            alt="" width="120" height="300"
            class="alignnone size-medium wp-image-915678" />
        <br><br>
       
        <button>Click</button>
         
          <script>
            $(document).ready(function () {
                $("button").click(function () {
                    $("img").attr("width", "300");
                });
            });
        </script>
    </center>
</body>
 
</html>

Output: 

 

Example 2: In this example, a pop-up will show the width of the image when the button is clicked.




<!DOCTYPE html>
<html>
 
<head>
    <title>jQuery attr() Method</title>
    <script src=
    </script>
</head>
 
<body>
    <center>
        <h1 style="color:green;">
            GeeksForGeeks
          </h1>
        <h2> jQuery attr() Method</h2>
        <h3 style="color:lightgreen;"></h3>
        <img src=
            alt="" width="120" height="300"
            class="alignnone size-medium wp-image-915678" />
        <br>
        <br>
        <button>Click</button>
        <script>
            $(document).ready(function () {
                $("button").click(function () {
                    alert("Image width: " +
                        $("img").attr("width"));
                });
            });
        </script>
    </center>
</body>
 
</html>

Output: 

 

Example 3: In this example, the image will become thin when the button is clicked.




<!DOCTYPE html>
<html>
 
<head>
    <title>jQuery attr() Method</title>
    <script src=
    </script>
</head>
 
<body>
    <center>
        <h1 style="color:green;">
            GeeksForGeeks
          </h1>
        <h2> jQuery attr() Method</h2>
        <h3 style="color:lightgreen;">
        </h3>
        <img src=
            alt="" width="120" height="300"
            class="alignnone size-medium wp-image-915678" />
        <br>
        <br>
        <button>Click</button>
        <script>
            $(document).ready(function () {
                $("button").click(function () {
                    $("img").attr("width", function (n, v) {
                        return v - 50;
                    });
                });
            });
        </script>
    </center>
</body>
 
</html>

Output:

 

Example 4: In this example, the image will shrink when the button is clicked.




<!DOCTYPE html>
<html>
 
<head>
    <title>jQuery attr() Method
    </title>
 
    <script src=
    </script>
</head>
 
<body>
    <center>
        <h1 style="color:green;">
            GeeksForGeeks
          </h1>
 
        <h2> jQuery attr() Method</h2>
        <h3 style="color:lightgreen;"></h3>
        <img src=
            alt="" width="120" height="300"
            class="alignnone size-medium wp-image-915678" />
        <br><br>
        <button>Click</button>
 
        <script>
            $(document).ready(function () {
                $("button").click(function () {
                    $("img").attr({
                        width: "150",
                        height: "100"
                    });
                });
            });
        </script>
    </center>
</body>
 
</html>

Output:

 


Article Tags :