Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7018197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:00:48+00:00 2026-05-27T23:00:48+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:00:49+00:00Added an answer on May 27, 2026 at 11:00 pm

    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/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a HTML code like this: <pre><div class=post style=border-width:1px;border-style:solid> <div class=userpic style=width:70px;float:left> <img
I have some SQL output that id like to format into a collapsible div.
How i can use append to append new line to div using javascript means
I have some div's like <div class=block-wrap> <a href=#>A</a> <a href=#>A</a> <a href=#>A</a> <a
I have a content editable div. Within this I am inserting a <pre>some code</pre>
I have some pre-existing js code that manipulates a div via DOM. I have
I have a div set to contentEditable and styled with white-space:pre so it keeps
I have a div with float:right (no position property declared). This div has some
I have a div with id=images. The div contains some images that are each
I have a div element containing some text. When the user clicks a word

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.