How to get html attribute's value in jquery?

Last updated 4 years, 5 months ago | 1221 views 75     5

Tags:- JQuery

Jquery | attr | Get HTML attribute's value

You can get the value of an attribute for the first element in the set of matched elements using the .attr() function.

 

Return the value of an attribute:

$(selector).attr(attribute)
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    var image_height = $("img").attr("height");
    alert(image_height);
  });
});
</script>
</head>
<body>

<img src="image.jpg" alt="Pulpit Rock" width="284" height="213"><br>

<button>Get the attribute height value</button>

</body>
</html>

Note: if there is a number of matched elements and To get the value for each element individually, use a looping construct such as jQuery's .each() or .map() method.