AppendChild in older IE browsers

I am trying to create a new element in my javascript code and add it to one of my elements. But it seems like older IE browsers don't support this feature and my code breaks down to a line where I use the appendChild function.

var child = document.createElement('span');
child.innerHTML = "Hello World!"; // Crashes here
parent.appendChild(child); // And here

      

Are there any alternatives that I can use for IE?

Thank.

PS the code works fine in modern browsers.

UPD: The following code solves the last part of my problem, I can add an empty parent parent:

var child = document.createElement('span');
if (parent.insertAdjacentElement){
   parent.insertAdjacentElement('beforeEnd', child);
}
else if (parent.appendChild) {
   parent.appendChild(child);
}

      

But I still need to put some data inside the child. createTextNode

. innerHTML

, setAttributes

do not work.

+2


source to share


4 answers


It may be unrelated, but please check this link for how someone else solved this problem



Edit: and this link too

+2


source


It seems to me that he crashed into the second line. innerHTML is written in lowercase i, not uppercase. It might even be your first line as there is a typo, but I guess only in the question.



0


source


Does this help solve the problem?

var child = document.createElement('span');
child.appendChild(document.createTextNode("Hello World!"));
parent.appendChild(child);

      

-1


source


parent.innerHTML

works fine.

-1


source







All Articles