How to get html attribute's value in jquery?

324 Views

How to get html attribute's value in 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 .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.

Tags:-
JQuery