The jQuery attr() method is used to set or return attributes and values of the selected elements.
There are two usage of jQuery attr() method.
To return attribute value: This method returns the value of the first matched element.
To set attribute value: This method is used to set one or more attribute/value pairs of the set of matched elements.
Note: Run below the example in you're deve tool.
There are two usage of jQuery attr() method.
To return attribute value: This method returns the value of the first matched element.
To set attribute value: This method is used to set one or more attribute/value pairs of the set of matched elements.
Note: Run below the example in you're deve tool.
- <!DOCTYPE html>
- <html>
- <head>
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
- <script>
- $(document).ready(function(){
- $("button").click(function(){
- $("img").attr("width", "500");
- });
- });
- </script>
- </head>
- <body>
- <img src="Image.jpg" alt="Good Morning Friends"width="284" height="213"><br>
- <button>Set the width attribute of the image</button>
- </body>
- </html>
- Convenience: When you use jQuery attr() method to get the value of the attribute of an element then it can be call directly on a jQuery object and chained to other jQuery methods.
- Cross-browser consistency: You can get rid from inconsistently changing of attribute?s value on different browsers or even on different versions of a single browser.
More Details: https://goo.gl/7GA1Zd