Get id of Button or Clicked Button with jQuery

Rumman Ansari   2022-12-17   Developer   web development > Get id of Button or Clicked Button with jQuery   460 Share

Get id of Button or Clicked Button with jQuery

GET ID OF BUTTON OR CLICKED BUTTON WITH JQUERY
Figure: GET ID OF BUTTON OR CLICKED BUTTON WITH JQUERY

html Code


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://code.jquery.com/jquery-3.6.2.min.js" integrity="sha256-2krYZKh//PcchRtd+H+VyyQoZ/e3EcrkxhM8ycwASPA=" crossorigin="anonymous"></script>
    <script src="script.js"> </script>
</head>
<body>
    <button id = "buttonId"> button Name </button>
</body>
</html>

Jquery Code


$(document).ready(function(){   
    
    $("#buttonId").click(function(){
        let id = $(this).attr("id");
        alert(id);
    }); 

});