DEV Community

Cover image for How to change buttons content in Javascript?
Lalit Kumar
Lalit Kumar

Posted on

How to change buttons content in Javascript?

When we create a button in HTML we give the button a name for example “click here”,”next” etc. Whatever we do to our page the text in the button remains the same but in this article we will change button text onclick.
Changing button text onclick means when we click on button the text on the button should change. For example: assume our button has “start” as text but after we click on button the text of the button should change to “stop”.

Approach

We will create a function to do that and assign it to the button. Here’s how

  • Create a new variable.
  • Get it by element id.
  • Check if the text is okay or you want to change it.
  • If you want to change it then assign the new text to it.
  • After that assign that function to the button. Alt Text

I have added code image but if you want to read more, check this

Hope this will help.

Top comments (0)