Categories
JavaScript Answers

How to click a button programmatically with JavaScript?

Spread the love

Sometimes, we want to click a button programmatically with JavaScript.

In this article, we’ll look at how to click a button programmatically with JavaScript.

How to click a button programmatically with JavaScript?

To click a button programmatically with JavaScript, we can call the click method.

For instance, we write

const userImage = document.getElementById("imageOtherUser");
const hangoutButton = document.getElementById("hangoutButtonId");
userImage.onclick = () => {
  hangoutButton.click();
};

to select the buttons with getElementById.

Then we add a click event handler to the userImage button by setting its onclick property to a function that calls hangoutButton.click.

This will click on the hangoutButton button programmatically.

Conclusion

To click a button programmatically with JavaScript, we can call the click method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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