2

I have created a new element with JavaScript using document.createElement() and I've appended it to the body using document.body.appendChild(). I want to create another element inside of this element inside of this element inside of this element. I know it can be done like this:

var firstElement = document.body.createElement('div');
var secondElement = document.body.cteateElement('div');
firstElement.appendChild(secondElement);
document.body.appendChild(firstElement);

But what if I don't want to append the second element to the first element immediately? What if I want to remove the element at a later time after some event? Wouldn't both elements need an ID?

I've created two functions, one to create the first element and one to create the second. I've then called them both:

function createFirstElement() {

};
function createSecondElement() {

};
createFirstElement();
createSecondElement();

inside of function createFirstElement(), I've created my first element, obviously! I've also styled it, given it an ID and appended it to the body:

function createFirstElement() {
    var firstElement = document.createElement('div');
    firstElement.id = 'firstElement';
    firstElement.style.width = '600px';
    firstElement.style.height = '400px';
    firstElement.style.backgroundColor = 'red';
    document.body.appendChild(firstElement);
};

This function works fine, a red <div> 600x400px is drawn in the browser. Now with createSecondElement(), I want to add another element inside of this element.

createElementTwo() is basically the same as createElementTwo(), except I want to create a new element inside of element one instead of the body:

function createElementTwo() {
    var firstElement = document.getElementById('firstElement');
    var elementTwo = document.createElement('div');
    elementTwo.id = 'elementTwo';
    elementTwo.style.width = '300px';
    elementTwo.style.height = '200px';
    elementTwo.backgroundColor = 'blue';
    firstElement.appendChild(elementTwo);
};

This does not work!

All together my code looks like this:

function init() {
    var body = document.body;

    function createFirstElement() {
        var firstElement = document.createElement('div');
        firstElement.id = 'firstElement';
        firstElement.style.width = '600px';
        firstElement.style.height = '400px';
        firstElement.style.backgroundColor = 'red';
        body.appendChild(firstElement);
    };
    function createElementTwo() {
        var firstElement = document.getElementById('firstElement');
        var elementTwo = document.createElement('div');
        elementTwo.id = 'elementTwo';
        elementTwo.style.width = '300px';
        elementTwo.style.height = '200px';
        elementTwo.backgroundColor = 'blue';
        firstElement.appendChild(elementTwo);
    };

    createFirstElement();
    createElementTwo();
};
init();

I've searched around online, but all I can find is how to create a new element, or add an element to an element that already exists in the HTML document.

What am I doing wrong?

0

2 Answers 2

2

It is fine, problem is in assigning background color to the second element - you has assigned backgroundColor to the dom element instead of assigning to its style

function init() {
  var body = document.body;

  function createFirstElement() {
    var firstElement = document.createElement('div');
    firstElement.id = 'firstElement';
    firstElement.style.width = '600px';
    firstElement.style.height = '400px';
    firstElement.style.backgroundColor = 'red';
    body.appendChild(firstElement);
  };

  function createElementTwo() {
    var firstElement = document.getElementById('firstElement');
    var elementTwo = document.createElement('div');
    elementTwo.id = 'elementTwo';
    elementTwo.style.width = '300px';
    elementTwo.style.height = '200px';
    elementTwo.style.backgroundColor = 'blue'; //here style was missed
    firstElement.appendChild(elementTwo);
  };

  createFirstElement();
  createElementTwo();
};
init();

Sign up to request clarification or add additional context in comments.

2 Comments

@eddmcmxciii your browser's DOM inspector would have shown you the second element was actually being appended properly
I didn't even think of that, thanks for the tip. I've tried accepting this answer, but apparently I have to wait 6 minutes.
1

I'd be inclined to return the created elements and pass the parent element in to createElementTwo so you don't have to worry about finding it by id. For example...

function createFirstElement() {
    // snip

    document.body.appendChild(firstElement);
    return firstElement;
}

function createElementTwo(firstElement) {
    var elementTwo = document.createElement('div');
    elementTwo.id = 'elementTwo';
    elementTwo.style.width = '300px';
    elementTwo.style.height = '200px';
    elementTwo.style.backgroundColor = 'blue'; // courtesy of Mr Johny
    firstElement.appendChild(elementTwo);
    return elementTwo;
}

createElementTwo(createFirstElement());

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.