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 clicked id js

<body>
    <main>
        <button id="first_button" onclick="getClickID(this.id)">First Button</button>
        <button id="second_button" onclick="getClickID(this.id)">Second Button</button>
        <button id="third_button" onclick="getClickID(this.id)">Third Button</button>
    </main>

    <script type="text/javascript">
        function getClickID(clickID) {
            alert(clickID);
        }
    </script>
</body>
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 :: fetch patch method 
Javascript :: javascript if field exists 
Javascript :: call function on modal open 
Javascript :: javascript get list of files in directory 
Javascript :: remove element from dictionary javascript 
Javascript :: org.json.JSONException: End of input at character 0 of 
Javascript :: discord.js send embed 
Javascript :: Deleting all white spaces in a string 
Javascript :: destructuring dynamic properties 
Javascript :: random string js 
Javascript :: jquery validation phone number 
Javascript :: nuxt lang 
Javascript :: unexpected token react native stack navigation 
Javascript :: javascript pdf preview 
Javascript :: generate random integer javascript 
Javascript :: mongoose check if string is objectid 
Javascript :: javascript parse json 
Javascript :: javascript dice throw 
Javascript :: nodejs request api 
Javascript :: throttling function in javascript 
Javascript :: DataTypes Time sequelize 
Javascript :: how to update kali linux on virtualbox 
Javascript :: angular elementref 
Javascript :: find array with children javascript 
Javascript :: how-to-save-array in Local storage - js 
Javascript :: video in react native stack overflow 
Javascript :: javscript foreach with click listener 
Javascript :: how to convert milliseconds to time in javascript 
Javascript :: node js performance is not defined 
Javascript :: get form data serialize jquery 
ADD CONTENT
Topic
Content
Source link
Name
3+5 =