jQuery attr() Method

jQueryWeb DevelopmentFront End Technology

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 −

raja
Published on 12-Nov-2019 11:35:06
Advertisements