jQuery attr() Method


The attr() method in jQuery is used to set or return attributes and values of the selected elements.

Syntax

The syntax is as follows −

$(selector).attr(attribute)

Example

Let us now see an example to implement the jQuery attr() method −

<!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(){
         alert("Input Type (Student Id) = " + $("input").attr("type"));
      });
   });
</script>
</head>
<body>
<h2>Student Details</h2>
Student Id: <input type="text"><br>
<button>Click me</button>
<p>Click on the button above to return the type of the input.</p>
</body>
</html>

Output

This will produce the following output −

Click on the above button to display the type of the input text −

Updated on: 12-Nov-2019

195 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements