Dynamically creating buttons in javascript using for loop, and assigning properties.

mjr_river picture mjr_river · Feb 2, 2017 · Viewed 11.4k times · Source

I am trying to use the below code to add buttons into the body of my page, the buttons appear, but none of the attributes display, it is a default button with no increase in size with font size, no text displayed, and no color.

When looking at the buttons using the console I can see the attributes are there, as shown below:

    <button id="btnA" innerhtml="A" value="A" text="A" font-size="14px"        background-color="#4CAF50"></button>

Not sure where I am going wrong here, any ideas?

-Thanks

Answer

kawadhiya21 picture kawadhiya21 · Feb 2, 2017

Everything is not an attribute, specially innerHTML.

btn.setAttribute("style","font-size:14px;background-color: #4CAF50");
btn.innerHTML = "A"