Yunath
Yunath

Reputation: 15

button.value is not changing the text value of button

I have this code in my javascript. It creates the button and has the onclick functionality but the .value doesn't work

var button = document.createElement("button");
button.value = "+";
button.onclick = addBox;
document.getElementById("section").appendChild(button);

Upvotes: 1

Views: 530

Answers (1)

Suren Srapyan
Suren Srapyan

Reputation: 68655

Not value but textContent. If you use value, you need to use <input type="button" />. Button's text is in the textContent.

Why textContent and not innerHTML, because textContent is faster, it not tries to parse into html.

var button = document.createElement("button");
button.textContent = "+";
document.getElementById("section").appendChild(button);
<div id="section">
  
</div>

For input with type=button

var button = document.createElement("input");
button.type = "button"
button.value = "+";
document.getElementById("section").appendChild(button);
<div id="section">
  
</div>

Upvotes: 1

Related Questions