I am writing HTML code that has a div that says y
, and that is in my body of HTML, and there is a button. when the user clicks on this button, I want to do the following.
- Create another div (with class
smallBar
) - Inside this
div
, I want to create 3 spaces. - Add this (generic
smallBar
) content to the div y
.
Here is my current code.
HTML
<div class="y" id="y">
</div>
<input type="button" onclick="createDiv()" value="click me" />
Js
function createDiv() {
var div = document.createElement("div");
div.className = "smallBar";
var span1 = document.createElement("span");
span1.className = "span1";
var span2 = document.createElement("span");
span2.setAttribute("class", "span1");
var span3 = document.createElement("span");
span3.setAttribute("class", "span1");
div.appendChild(span1);
div.appendChild(span2);
div.appendChild(span3);
document.getElementById("y").innerHTML = div;
}
here, when I click on the button, instead of creating div
it gives me a message like [object HTMLDivElement]
. Let me know where I am wrong and how I can fix it.
Here is my fiddle https://jsfiddle.net/d9hg0vkk/
thank
source
share