Getting the first element from <body> then insertBefore()

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
spedula
Forum Commoner
Posts: 81
Joined: Mon Mar 29, 2010 5:24 pm

Getting the first element from <body> then insertBefore()

Post by spedula »

Hi, I've been at this for a few hours now and am about to start ripping hair out. Basically what I need to do is get the first element that appears in the body and then insert another element before it.

I've tried the following to get the first element with no success (undefined or null)

Code: Select all

window.document.body.firstChild

document.getElementsByTagName("body").firstChild

document.getElementsByTagName("body")[0].firstChild

window.document.documentElement.childNodes[1].childNodes[0]
And a whole slew of mixed and matched attempts of the previous snippets. I've also tried just getting the body then appendChild() with no success either.

Any help here is appreciated. Thanks in advance.
Post Reply