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

  • SEARCH
  • Home
  • 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 7702305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:11:50+00:00 2026-05-31T23:11:50+00:00

When I do something like divNode.appendChild(pNode) , the pNode is moved inside divNode .

  • 0

When I do something like divNode.appendChild(pNode), the pNode is moved inside divNode. It seems like its parent changes from whatever it was to divNode. Is this an expected behavior?

Here is my code: http://jsfiddle.net/YqG96/

<html>
<head>
<style>
p {
    background: orange;
}
div {
    background: lightcyan;
    padding: 5px;
}
</style>
<script>
window.onload = function() {
    pNode = document.getElementById('pNode');
    divNode = document.getElementById('divNode');
    divNode.appendChild(pNode);
}
</script>
</head>
<body>
<p id="pNode">Foo</p>
<div id="divNode">Bar</div>
</body>
</html>

Is it possible to modify it so that pNode remains under the body as well as the divNode? If two parents for a node doesn’t make sense, then is it possible to create a new copy of pNode and append this new copy to divNode?

  • 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-31T23:11:52+00:00Added an answer on May 31, 2026 at 11:11 pm

    This is an expected behavior. When you do appendChild()on a node that is already in the DOM, the node is moved from where it currently is in the DOM to the new location you specify. That will change it’s parent and the next and previous nodes too.

    A node can only have one direct parent, so it is not possible to have two parents as each parent signifies a location for the node and, unlike in the mysteries of particle physics, a node can only be one place at a time. If you want two nodes in two locations, you can either create a new node and append that one or you can clone the existing node and append that one.

    See the cloneNode() function for info about how to make a copy of the node and insert the copy in this second location.

    <script>
    window.onload = function() {
        pNode = document.getElementById('pNode').cloneNode(true);
        pNode.id = "pNode2";
        divNode = document.getElementById('divNode');
        divNode.appendChild(pNode);
    }
    </script>
    

    Remember that when you clone nodes, any nodes in the cloned tree that had an ID will need a new ID so that ID values stay unique in the document.

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

Sidebar

Related Questions

Something like this: from Tkinter import * root = Tk() but = Button(root, text
Something like this: SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE CONSTRAINT_NAME ='FK_TreeNodesBinaryAssets_BinaryAssets' and TABLE_NAME = 'TreeNodesBinaryAssets'
Something like... $('.foo').live 'click', -> setTimeout (()->$(this).parent().hide()), 5000 Thanks for any help!
Does something like this work SELECT color FROM rawdata WHERE color LIKE ( SELECT
Something like: for (a,b) in kwargs.iteritems(): if not b : del kwargs[a] This code
Something like this: if self.__class__ == User: logging.debug(%s non_pks were found % (str(len(non_pks))) )
Something like: SELECT MONTH(*date?*) FROM //Not sure what goes here LIMIT 0,6 I know
Something like this: sass --watch style.css:(other than root directory) //or sass --watch (other directory
Is something like this possible: function FooClass() { var barsArray=new Array(); var index=0; function
Something like this (yes, this doesn't deal with some edge cases - that's not

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.