Skip to content
Fix Code Error

JavaScript – onClick to get the ID of the clicked button

March 13, 2021 by Code Error
Posted By: Anonymous

How do find the id of the button which is being clicked?

<button id="1" onClick="reply_click()"></button>
<button id="2" onClick="reply_click()"></button>
<button id="3" onClick="reply_click()"></button>

function reply_click()
{
}

Solution

You need to send the ID as the function parameters. Do it like this:

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

This will send the ID this.id as clicked_id which you can use in your function. See it in action here.

Answered By: Anonymous

Related Articles

  • Is CSS Turing complete?
  • Setting display to none with Javascript
  • Azure CLI - az deployment group create -…
  • How to create an alert the fade after a duration in Vuetify?
  • setTimeout function not working : javascript
  • Azure Availability Zone ARM Config
  • How to use html template with vue.js
  • Onclick, the button moves down, why can that due to?
  • How to use setTimeout on vuex action?
  • Active tab issue on page load HTML

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

Vertically align text next to an image?

Next Post:

How to merge two arrays in JavaScript and de-duplicate items

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Get code errors & solutions at akashmittal.com
© 2022 Fix Code Error