Skip to content
Advertisement

Changing display text on a button and back again using Javascript [closed]

I’m been trying multiple attempts to change the text on a button so that when the form opens it shows the text “Add Challenging Questions” and if you click it, it unhides a div, but then I want the text of the button to change to “Remove Challenging Questions” and if it’s clicked it hides that div again. The code I have works with regards to showing and hiding the div, but nothing I’ve tried affects the text of the button.

I’ve also tried buttonName.value and buttonName.innerText. The developer tools show a “uncaught type error” and says it cannot set the property. Any idea what is wrong?

JavaScript
JavaScript

Advertisement

Answer

You have a typo (missing quotes in document.getElementById(‘cqButton’))

But you also need to delegate

I wrapped the buttons in a div. When something in the div is clicked, I test to see if it has class cqButton. If it does, I get the ID of the div to show from the data attribute and toggle the hide class on that div. then I look to see if the div I just toggled has the hide class or not to determine the text in the button

JavaScript
JavaScript
JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement