jQuery attr() Method

jQuery gives us the means to manipulate the properties of the HTML elements. We can modify the attributes later on after getting access to those properties. You can use the jQuery attr() method to get the value of an attribute for the first element in the set of matched elements or set one or more attributes for every matched element. It works similarly to the .css() method, except with .attr(), you're not setting or changing the style rules, but the inline HTML attributes of a particular element.

jQuery Get Attribute

var alt = $("#imgID").attr("alt") alert(alt);
<img id="imgID" src="dummy.png" alt="Not set" />

jQuery Set Attribute

$("#imgID").attr("alt", "New Attribute..");
<img id="imgID" src="dummy.png" alt="Not set" />
run this source code Browser View

Not set
Full Source
<html> <head> <title>jQuery attr() example</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script> $(document).ready(function() { $("#btnGet").click(function(){ var alt = $("#imgID").attr("alt") alert(alt); }); $("#btnSet").click(function(){ $("#imgID").attr("alt", "New Attribute.."); }); }); </script> </head> <body> <button id="btnGet">Get Val</button> <button id="btnSet">Set Val</button></br> <img id="imgID" src="dummy.png" alt="Not set" /> </body> </html>