DEV Community

Walktheworld
Walktheworld

Posted on

How to Deactivate / Disable Button after Click

So I am working on my first Javascript/HTML project for coding school. I made it so that when the user clicks on a button it would render some API data onto the page. But if the user clicked that same button more than once it would add the same data to the page with every click.

First I tried to see if I could find a way to dynamically disable the button with JS code without having to write a whole function. I tried to use the btn.id = "" format with .onclick while dynamically creating the button and that did not work.

Alt Text

The only solution I got to work was to add a disableButton() function, create the button in the HTML file and add an onclick classification inside of its tag activating that JS function.

Alt Text

Top comments (0)