86

I would like to append an li element after another li inside a ul element using javascript, This is the code I have so far..

var parentGuest = document.getElementById("one");
var childGuest = document.createElement("li");
childGuest.id = "two";

I am familiar with appendChild,

parentGuest.appendChild(childGuest);

However this appends the new element inside the other, and not after. How can I append the new element after the existing one? Thanks.

<ul>
  <li id="one"><!-- where the new li is being put --></li>
  <!-- where I want the new li -->
</ul>
2
  • 1
    Show some html.. is the parent a ul or li? Commented Aug 31, 2011 at 14:18
  • I voted to reopen this QA because I want to add new updated answer as this QA still pops up as top search result on google. Commented May 19, 2020 at 20:32

8 Answers 8

120

You can use:

if (parentGuest.nextSibling) {
  parentGuest.parentNode.insertBefore(childGuest, parentGuest.nextSibling);
}
else {
  parentGuest.parentNode.appendChild(childGuest);
}

But as Pavel pointed out, the referenceElement can be null/undefined, and if so, insertBefore behaves just like appendChild. So the following is equivalent to the above:

parentGuest.parentNode.insertBefore(childGuest, parentGuest.nextSibling);
Sign up to request clarification or add additional context in comments.

1 Comment

if-else is unnecessary: the second arg of insertBefore can be null or undefined
20

If you are looking for a plain JS solution, then you just use insertBefore() against nextSibling.

Something like:

parentGuest.parentNode.insertBefore(childGuest, parentGuest.nextSibling);

Note that default value of nextSibling is null, so, you don't need to do anything special for that.

Update: You don't even need the if checking presence of parentGuest.nextSibling like the currently accepted answer does, because if there's no next sibling, it will return null, and passing null to the 2nd argument of insertBefore() means: append at the end.

Reference:

.

IF you are using jQuery (ignore otherwise, I have stated plain JS answer above), you can leverage the convenient after() method:

$("#one").after("<li id='two'>");

Reference:

3 Comments

excuse my ignorance but how does this implement with javascript? could you show how it will work with my current code, thanks.
I think if you are not using jQuery for it, then Yoshi's answer is right. It is also mentioned here netlobo.com/javascript-insertafter.html - I just tended to assume jQuery is super default. My mistake :D
A year and half later and I get a vote up for this answer. So, I have updated it to include the shortest possible version of the plain JS solution, and a link to Mozilla docs on it. Sorry for not doing it earlier (since others did it already with very similar implementations, I didn't think I needed to).
18

You need to append the new element to existing element's parent before element's next sibling. Like:

var parentGuest = document.getElementById("one");
var childGuest = document.createElement("li");
childGuest.id = "two";
parentGuest.parentNode.insertBefore(childGuest, parentGuest.nextSibling);

Or if you want just append it, then:

var parentGuest = document.getElementById("one"); 
var childGuest = document.createElement("li"); 
childGuest.id = "two"; 
parentGuest.parentNode.appendChild(childGuest);

Comments

6

after is now a JavaScript method

MDN Documentation

Quoting MDN

The ChildNode.after() method inserts a set of Node or DOMString objects in the children list of this ChildNode's parent, just after this ChildNode. DOMString objects are inserted as equivalent Text nodes.

The browser support is Chrome(54+), Firefox(49+) and Opera(39+). It doesn't support IE and Edge.

Snippet

var elm=document.getElementById('div1');
var elm1 = document.createElement('p');
var elm2 = elm1.cloneNode();
elm.append(elm1,elm2);

//added 2 paragraphs
elm1.after("This is sample text");
//added a text content
elm1.after(document.createElement("span"));
//added an element
console.log(elm.innerHTML);
<div id="div1"></div>

In the snippet, I used another term append too

Comments

3

Use the parent element children array.
In the example below we are adding a new element after the first child element (hence we will insert the new element before the element in index 1):

parentElement.insertBefore(newElement, parentElement.children[1]);

Comments

1

This suffices :

 parentGuest.parentNode.insertBefore(childGuest, parentGuest.nextSibling || null);

since if the refnode (second parameter) is null, a regular appendChild is performed. see here : http://reference.sitepoint.com/javascript/Node/insertBefore

Actually I doubt that the || null is required, try it and see.

Comments

1

You could also do

function insertAfter(node1, node2) {
    node1.outerHTML += node2.outerHTML;
}

or

function insertAfter2(node1, node2) {
    var wrap = document.createElement("div");
    wrap.appendChild(node2.cloneNode(true));
    var node2Html = wrap.innerHTML;
    node1.insertAdjacentHTML('afterend', node2Html);
}

Comments

0

If you can get the parent element, just use the insertBefore method with childNodes prop (for me the easiest way, by far).

I will left here a complete comparison.

Original question code:

var parentGuest = document.getElementById("one");
var childGuest = document.createElement("li");
childGuest.id = "two";
childGuest.textContent = "two";
parentGuest.appendChild(childGuest);
<ul>
  <li id="one"><!-- where the new li is being put -->one</li>
  <!-- where I want the new li -->
</ul>

Now we change appendChild by insertBefore with childNodes:

var parentGuest = document.getElementById("parent-el");
var childGuest = document.createElement("li");
childGuest.id = "two";
childGuest.textContent = "two";
parentGuest.insertBefore(childGuest, parentGuest.childNodes[2]);
<ul id="parent-el">
  <li id="one"><!-- where the new li is being put -->one</li>
  <!-- where I want the new li -->
</ul>

The cool thing about this solution is that you can move freely between child nodes just by changing the index of the childNodes.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.