how to get id of html element in jquery code example

Example 1: jquery get element id

$(this).attr('id')

Example 2: get the id of a div in jquery

var rid = $(this).attr('id');	//for dynamically created elements

Example 3: jquery get element by id

<script>
$(document).ready(function(){
    $("#myBtn").click(function(){
        var elmId = $("#test").attr("id");
        alert(elmId);
    });
});
</script>

<body>
    <div id="testDiv">Data of the Div</div>
    <br>
    <button type="button" id="myBtn">Show Div ID</button>
</body>