29

So I have a div with some pre tags in it, like so:

<div id="editor" > <pre contentEditable="true">1</pre> <pre contentEditable="true">2</pre> <pre contentEditable="true">3</pre> </div> 

Now I want to use Javascript to put a new pre node between 1 and 2. I've been trying to do it this way (since I understand the DOM is a doubly linked tree), but I'm getting the sense that maybe the pointers aren't editable as I'm approaching it.

(just a snippet inside an event handler, e being the event)

var tag = e.srcElement; if(tag.nextSibling){ var next = tag.nextSibling; var newPre = document.createElement('pre'); newPre.setAttribute("contentEditable", "true"); newPre.innerHTML = "boom"; tag.nextSibling = newPre; newPre.nextSibling = next; } 

Those last two lines are from my c++ experience, but feel icky in JS. How would I set a new sibling node?

3 Answers 3

51

Here is how I would do that:

JS

var container = document.getElementById('editor'), firstChild = container.childNodes[1]; if (container && firstChild) { var newPre = document.createElement('pre'); newPre.setAttribute("contentEditable", "true"); newPre.innerHTML = "boom"; firstChild.parentNode.insertBefore(newPre, firstChild.nextSibling); } 

jsfiddle: http://jsfiddle.net/bZGEZ/

Sign up to request clarification or add additional context in comments.

1 Comment

Possible improvements: 1) use 'children' (only elements) instead of 'childNodes' (elements and text nodes); 2) check container's children in 'if': container can be null; 3) use native HTMLElement 'contentEditable' property instead of setAttribute; 4) use 'textContent' instead of 'innerHTML' if you don't require parsing value as HTML (as in this case); 5) use simpler DOM attach methods 'after' and 'before' (most browsers except IE11 support them).
34

You could also insert a new sibling using insertAdjacentElement or insertAdjacentHTML; both of which take the options beforebegin, beforeend, afterbegin and afterend.

Example:

var container = document.getElementById('editor'), firstChild = container.childNodes[1]; var newPre = document.createElement('pre'); newPre.setAttribute("contentEditable", "true"); newPre.innerHTML = "boom"; firstChild.insertAdjacentElement("afterend", newPre); 

1 Comment

I've been programming for years, and this is the first time I've heard of insertAdjacentElement, and it is absolutely magnificent! Thank you!
0

There's also the Element.after() method. The benefit of this method over the other methods is that it allows you to add multiple nodes at the same time.

const container = document.getElementById('editor'); const firstChild = container.childNodes[1]; const newPre1 = document.createElement('pre'); newPre1.setAttribute('contentEditable', 'true'); newPre1.innerHTML = 'foo'; const newPre2 = document.createElement('pre'); newPre2.setAttribute('contentEditable', 'true'); newPre2.innerHTML = 'bar'; firstChild.after(newPre1, newPre2); 

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.