I am facing an issue where my JavaScript code breaks in old IE browsers while trying to create a new element and append it to another element using the appendChild function.
var child = document.createElement('span');
child.innerHTML = "Hello World!"; // Code fails here
parent.appendChild(child); // And here
Are there any alternatives that I can use for IE?
Thank you.
P.S. The code works perfectly fine in modern browsers.
UPDATE: I have managed to partially solve the issue by appending an empty child to a parent:
var child = document.createElement('span');
if (parent.insertAdjacentElement){
parent.insertAdjacentElement('beforeEnd', child);
}
else if (parent.appendChild) {
parent.appendChild(child);
}
However, the problem still persists when trying to add data inside the child element. Methods like createTextNode
, innerHTML
, and setAttributes
do not work.