Categories
JavaScript Answers

How to change onclick attribute with JavaScript?

Spread the love

To change onclick attribute with JavaScript, we set the onclick property.

For instance, we write

document.getElementById(id).onclick = () => {
  //...
};

to get the element with getElementById.

Then we set its onclick property to the click handler to set the onclick attribute value.

By John Au-Yeung

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

Leave a Reply

Your email address will not be published.