0

I have a button in my html that I append. How do I set said button's text to "Click Me"? I've scoured every corner of Stack Overflow and to no avail.

var button = container.appendChild(document.createElement("button");
button.setAttribute("id", button);

Here's what I've already tried:

button.text="Click Me"
button.innerHTML="Click Me";
button.value="Click Me";
button.childNodes[0].nodeValue="Click Me";

Any help will be appreciated!

4
  • 3
    innerHTML or innerText should have worked. Commented Feb 10, 2020 at 15:27
  • 2
    Not exactly related, but button.setAttribute("id", button); probably doesn't do what you expect. Maybe you should wrap button in the argument list into quotes. Currently the id is [object HTMLButtonElement]. Commented Feb 10, 2020 at 15:28
  • 2
    You're missing a ) on the var button = line, so the button isn't created. After fixing the typo, it works with button.innerHTML. See jsfiddle.net/barmar/qsk8ahej/1 Commented Feb 10, 2020 at 15:31
  • 1
    did you notice syntax error? Commented Feb 10, 2020 at 15:31

1 Answer 1

1

Missing closing parenthesis:

.appendChild( ... )

var container = document.getElementById("container");

var button = container.appendChild(document.createElement("button"));

button.innerHTML="Click Me!!";
<div id="container"></div>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.