Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

get id of clicked element javascript

<button id="3" onClick="reply_click(this.id)">B3</button>
    
<script type="text/javascript">
  function reply_click(clicked_id)
  {
      alert(clicked_id);
  }
</script>
Comment

how to get id of current element clicked

//JavaScript
document.querySelectorAll('div').forEach((e) => {
  e.onclick = (e) => console.log(e.currentTarget.id);
});
Comment

Get the ID of the clicked button using JavaScript

<!DOCTYPE HTML> 
<html> 
    <head> 
        <title> 
            Get the ID of the clicked button
            using JavaScript
        </title>
    </head> 
      
    <body style = "text-align:center;"> 
      
        <h1 style = "color:green;" > 
            GeeksForGeeks 
        </h1>
  
        <p id = "GFG_UP" style = 
            "font-size: 15px; font-weight: bold;">
        </p>
          
        <button id="1" onClick="GFG_click(this.id)">
            Button1
        </button>
          
        <button id="2" onClick="GFG_click(this.id)">
            Button2
        </button>
          
        <button id="3" onClick="GFG_click(this.id)">
            Button3
        </button>
  
        <p id = "GFG_DOWN" style = 
            "color:green; font-size: 20px; font-weight: bold;">
        </p>
          
        <script>
            var el_up = document.getElementById("GFG_UP");
            var el_down = document.getElementById("GFG_DOWN");
            el_up.innerHTML = "Click on button to get ID";
          
            function GFG_click(clicked) {
                el_down.innerHTML = "ID = "+clicked;
            }         
        </script> 
    </body> 
</html>
Comment

PREVIOUS NEXT
Code Example
Javascript :: https://mongoosejs.com/docs/deprecations.html#findandmodify 
Javascript :: make first letter capital 
Javascript :: boilerplate node js server 
Javascript :: javascript operator double pipes 
Javascript :: socket io close client connection 
Javascript :: js map constructor 
Javascript :: Use the parseInt Function with a Radix 
Javascript :: js id generator 
Javascript :: how to preview a pdf document in react 
Javascript :: firebase firestore delete field 
Javascript :: sls invoke local 
Javascript :: javascript remove class with transition 
Javascript :: remove a object name from spread operator 
Javascript :: alert and prompt in javascript 
Javascript :: javascript string contains character 
Javascript :: custom event handler javascript 
Javascript :: javascript calculate 24 hours ago 
Javascript :: populate dropdown using jquery from database 
Javascript :: convert string to number javascript 
Javascript :: two sum javascript 
Javascript :: moment js difference between two dates 
Javascript :: foreach javascript 
Javascript :: how to remove all child elements in javascript 
Javascript :: how to create a server in node js 
Javascript :: how to print numbers from 1 to 100 in javascript 
Javascript :: js test undefined 
Javascript :: how to call a function with arguments on event listener javascript 
Javascript :: check if element is visible or hidden in dom 
Javascript :: how to reset input field in javascript 
Javascript :: react form submit 
ADD CONTENT
Topic
Content
Source link
Name
6+6 =