javascript click button by id – how to call javascript function based on button click?

javascript click button by id – Once you have the Id of the control that you want to programmatically click, you can use the JavaScript .click() function to trigger a button.

javascript click button by id

When the user clicks the given button, the corresponding function will get executed, and an alert dialog box will be displayed on the screen. The JavaScript function will take the selected button element as object inside the variable named ‘btn’.

Read Also:  Reusable jquery AJAX requests Multiple Times

javascript click button by id

var button = document.getElementById("button");
button.click();

get id of clicked element javascript

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

Get ID of Clicked Element using JavaScript

HTML Code

<!DOCTYPE html>
<html>
<head>
    <title>Get ID of Clicked Element using JavaScript - www.pakainfo.com</title>
</head>
<body>
    <button id='455821' onclick="getProductDetails(this)">Mobile 1</button>
    <button id='787878' onclick="getProductDetails(this)">Laptop 2</button>
</body>
</html>

JavaScript Code

<script>
function getProductDetails(btn)
{
alert(btn.id);
}
</script>

Don’t Miss : how to call a function in react with arguments onclick?

I hope you get an idea about javascript click button by id.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.