Reputation: 15
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
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