using javascript's .insertBefore to insert item as last child
Asked Answered
M

6

10

I so miss jQuery. I'm working on a project where I need to get my hands dirty with good 'ol plain Javascript again.

I have this scenario:

parent
    child1
    child2
    child3

Via javascript, I want to be able to insert a new node before or after any of those children. While javascript has an insertBefore, there is no insertAfter.

Insert before would work fine on the above to insert a node before any one of those:

parent.insertBefore(newNode, child3)

But how does one insert a node AFTER child3? I'm using this at the moment:

for (i=0,i<myNodes.length,i++){
    myParent.insertBefore(newNode, myNodes[i+1])
}

That is inserting my newNode before the next sibling node of each of my nodes (meaning it's putting it after each node).

When it gets to the last node, myNodes[i+1] become undefined as I'm now trying to access a array index that doesn't exist.

I'd think that'd error out, but it seems to work fine in that in that situation, my node is indeed inserted after the last node.

But is that proper? I'm testing it now in a few modern browsers with no seemingly ill effects. Is there a better way?

Melanism answered 2/3, 2011 at 21:12 Comment(2)
Why you don't use .appendChild?Bronchopneumonia
Can we get the accepted answer adjusted here? @Gibolt has the most appropriate answer for current standards and would help any future readers greatly.Victorie
H
24

The functionality of insertBefore(newNode, referenceNode) is described as:

Inserts the specified node before a reference node as a child of the current node. If referenceNode is null, then newNode is inserted at the end of the list of child nodes.

And since myNodes[i+1] is outside of the array bounds, it returns undefined, which is treated as null in this case. This means you get your desired behavior.

Edit: Link to the W3 specification of insertBefore()

Harping answered 4/3, 2011 at 10:55 Comment(2)
Good to know! I guess it's perfectly valid.Melanism
While this does work, for any future readers: Check out the answer provided by @Gibolt as it is the most up to date and intuitive way of doing this if you do not need to support legacy browsers such as IE.Victorie
R
31

Pure JavaScript actually has a method for what you want:

parent.appendChild(child_to_be_last)

Referendum answered 5/10, 2014 at 9:38 Comment(1)
This really seems like the most proper way to do itWormeaten
H
24

The functionality of insertBefore(newNode, referenceNode) is described as:

Inserts the specified node before a reference node as a child of the current node. If referenceNode is null, then newNode is inserted at the end of the list of child nodes.

And since myNodes[i+1] is outside of the array bounds, it returns undefined, which is treated as null in this case. This means you get your desired behavior.

Edit: Link to the W3 specification of insertBefore()

Harping answered 4/3, 2011 at 10:55 Comment(2)
Good to know! I guess it's perfectly valid.Melanism
While this does work, for any future readers: Check out the answer provided by @Gibolt as it is the most up to date and intuitive way of doing this if you do not need to support legacy browsers such as IE.Victorie
N
11

Modern Solution

If you want to position based on child, simply use before or after

child1.before(newNode)   // [newNode, child1, child2]
// or
child1.after(newNode)    // [child1, newNode, child2]

If you want to position based on parent, use prepend or append

parent.prepend(newNode)  // [newNode, child1, child2]
// or
parent.append(newNode)   // [child1, child2, newNode]

Advanced usage

  1. You can pass multiple values (or use spread operator ...).
  2. Any string value will be added as a text element.

Examples:

child1.after(newNode, "foo")     // [child1, newNode, "foo", child2]

const list = ["bar", newNode]
parent.prepend(...list, "fizz")  // ["bar", newNode, "fizz", child1, child2]

Mozilla Docs

before - after

prepend - append

Can I Use - 95% Mar 2021

Noblesse answered 13/8, 2017 at 2:55 Comment(2)
Thank you so much for this! I do not feel like this is very well known in the current standard and this should now be the accepted answer. It is accepted by all major, non-legacy (meaning excluding IE, but who cares), browsers.Victorie
same here @Victorie very much appreciate you sharing this, I have not come across these methods before!Putty
R
6

To insert item as a last node use :parentNode.insertBefore(newNode, null);

Rewrite answered 27/12, 2012 at 22:4 Comment(0)
C
1

Also when not iterating through children (just inserting after referenceNode) this might be useful:

parentNode.insertBefore(newNode, referenceNode.nextSibling);
Carvel answered 14/9, 2012 at 21:40 Comment(0)
P
0

I got this code is work to insert a link item as the last child

var cb=function(){
    //create newnode
    var link=document.createElement('link');
    link.rel='stylesheet';link.type='text/css';link.href='css/style.css';

    //insert after the lastnode
    var nodes=document.getElementsByTagName('link'); //existing nodes
    var lastnode=document.getElementsByTagName('link')[nodes.length-1]; 
    lastnode.parentNode.insertBefore(link, lastnode.nextSibling);
};

var raf=requestAnimationFrame||mozRequestAnimationFrame||
    webkitRequestAnimationFrame||msRequestAnimationFrame;
if (raf)raf(cb);else window.addEventListener('load',cb);
Portiere answered 9/4, 2015 at 2:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.