0

Structuring div's in HTML goes like this:

<div id="parentdiv">
     <div id="childdiv"></div>
</div>

My code bellow creates 2 divs, but places the child div next to the parent div instead of inside it:

var pardiv = document.createElement('pardiv');
pardiv.setAttribute('id', "workspace");
pardiv.innerHTML = "Hello World";
pardiv.style.background = "red";
pardiv.width = 300;
pardiv.height = 300;
document.body.appendChild(pardiv);

var childdiv = document.createElement('childdiv');
    childdiv.setAttribute('id', "childdiv");
    childdiv.innerHTML = "Hello World";
    childdiv.style.background = "yellow";
    childdiv.width = 100;
    childdiv.height = 100;
    document.body.appendChild(childdiv);

How can a programmer structure div's like in HTML if they are created through JavaScript?

1 Answer 1

3

Don't append the childDiv to the body, instead, append it to the parent element like so:

pardiv.appendChild(childdiv);

Using that, you should be able to set up the same structure as you would in HTML.

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.