Last Updated on April 18, 2023 by Roshan Parihar
jQuery get id of an HTML elements using jQuery attr() method. You just need to put the id as an argument of the attr method.
If you want to learn more about the jQuery attr method syntax and other details, you have to visit jQuery get attr() method page.
jQuery Get id of div element
See the below given example to learn the method and get id.
1 2 3 4 5 6 7 8 9 |
<script> $(document).ready(function(){ $("#btn-getid").click(function(){ alert($("#div-getid").attr("id")); }); }); </script> <button id="btn-getid" class="btn btn-primary">Get Id</button> <div id="div-getid">This is the answer to get id of an HTML element using jQuery attr() method.</div> |
Output
This is the answer to get id of an HTML element using jQuery attr() method.
The above example gives you the id of the HTML div element. Click the button given above to get the id in the alert part of the jquery.
If you to change the attribute value then see the jQuery set attr() method.
You must also read.