0

I am trying to add a node after the first child node as well as after the last child node of body tag.

I tried it in jsfiddle.com: http://jsfiddle.net/satishkmr/7MM52/3/

A condition is checked in between for text node.

   if(nextSibOfRefElem.nodeType == 3 )
    {
            nextSibOfRefElem = nextSibOfRefElem.nextSibling
            alert(nextSibOfRefElem.innerHTML)
    }

But in case of adding last child node of body, it does not interpret after the if condition(line no. 7 of javascript section). But while commenting out the if condition it works i.e. it gets appended to the last node. What is wrong with the if condition when nextSibling is null. Please help to sort it out.

Satish
  • 1,315
  • 4
  • 15
  • 22
  • Check your console for errors - specifically, what happens when `nextSibOfRefElem` is `null` and you try to access a `nodeType`? – RobH Feb 14 '14 at 11:47
  • For adding a node “after the last child”, just use `appendChild`. – CBroe Feb 14 '14 at 12:39

0 Answers0