Wednesday, July 23, 2008

insertAdjacentElement for FireFox

There is a JavaScript method called insertAdjacentElement() that only works in Internet Explorer (IE) and it is NOT supported in FireFox. There is an alternative however. Here is how you could use insertAdjacentElement() to insert a newly created element to the right of a specified element that already exists on the page: var newElem = document.createElement("span"); newElem.innerHTML = 'this is a new element here'; existingElement.insertAdjacentElement("AfterEnd", newElem); If you are want your code to work in IE and FireFox, try replacing the last line with: srcElement.parentNode.insertBefore(newElem, existingElement.nextSibling); If the browser you are using doesn't support that, you can try replacing the last line with the following (it is the same sometimes, but sometimes not) existingElement.parentNode.appendChild(newElem);


Oscar Islas L. said...

Gracias :D pocas veces encuentro que FF no haga algo que si IE, pero tu post me ayudo de forma es excelente.

Brent V said...

Hi Oscar,

My spanish is a little rusty, so I used a translator program on the web. I think I got the idea of what you said.

I'm glad it helped. Thank you for the kind feedback.