Skip to content Skip to sidebar Skip to footer

Why I Couldn't Append A Child Node To The Document Object Model Directly?

While studying the Document object model today, I faced a problem of appending a newly created child on the document object directly, here is my code : var newEl=document.createEl

Solution 1:

This may be what you're looking for if what you want to do is understand the spec and constraints surrounding the different items in the Node Tree. Basically, the Document can have only 1 type (HTML or XML) and 1 element/child (e.g <html>) and the element (<html> tag) can have multiple children (<body>, <head>, etc..), attributes, etc.. So the element (<html>) and its children can be appended to, but the document itself cannot.

Hope that helps.

Solution 2:

I just had this error so wanted to clarify the reason , What you are doing using this command document.appendChild(newEl);, is trying to add an element to the main document, but the main document has only one element allowed which is the main <html> tag.

So what you can do is either pick another element, or if you want to add it to the displayable area you should use document.body.appendChild(newEl)

Post a Comment for "Why I Couldn't Append A Child Node To The Document Object Model Directly?"