How to use HTML Attr Method in JQuery

This article describe about HTML Attr Method in JQuery.
  • 2442

HTML attr() method in jQuery

The arre() method is return the value of selected elements and sets the attribute value.

 Attr method is process to work different parameters.

Syntax

$(selector).attr(attribute)

Example

<!DOCTYPE html>

<html>

<head>

<script type="text/javascript" src="jquery.js"></script>

<script type="text/javascript">

    $(document).ready(function ()

{

        $("button").click(function ()

 {

            $("img").attr("width", "150");

        });

    });

</script>

</head>

<body>

<img src="Blue hills.jpg" alt="Pulpit Rock" width="284" height="213" />

<br />

<button>Set the width attribute of the image</button>

</body>

</html>

Output1

op1.jpg

Output2 After click this button

op2.jpg

Using a Function

We use a function to set the attribute value for the selected elements.

Syntax

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

You may also want to read these related articles here

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

© 2020 DotNetHeaven. All rights reserved.