Question

I am trying to create a new Element in my javascript code and append it to one of my elements. But it seems that old IE browsers do not support this function and my code breaks on the line where I use appendChild function.

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

Is there any alternatives I can use for IE?

Thank you.

P.S the code works fine in modern browsers.

UPD: The following code solves last part of my problem, I can append empty child to a 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 of child element. createTextNode, innerHTML, setAttributes do not work.

Was it helpful?

Solution

This might be unrelated, but check this link for how someone else has solved this issue

Edit: and this link too

OTHER TIPS

Looks to me like it's crashing on the 2nd line. innerHTML is written with a lower-case i, not upper case. It could even be your first line as there's a typo, but I assume that's just in the question.

Does this fix the problem?

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

parent.innerHTML seems to work fine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top