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

jQueryWeb DevelopmentFront End Technology

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>
raja
Published on 08-Oct-2018 16:25:31
Advertisements