How can I get the ID of an DOM element using jQuery?


In jQuery attr method is use to get the id attribute of the first matching element.

$("btn1").attr("id")

In your example

Example

<html >
<head>
<title>iddom</title>
   <script src="../../Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
      <script type="text/javascript">
      $(document).ready(function () {
         $('#btn1').click(function () {
            alert( $('#test').attr('id'));
         });
      });
   </script>
</head>
<body>
   <div id="test">
   </div>
      <button type="button" id="btn1">Click Here</button><br />
</body>
</html>

Updated on: 22-Jun-2020

569 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements