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 6014745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:44:14+00:00 2026-05-23T02:44:14+00:00

I am trying to replace a certain div element parent with another one newparent

  • 0

I am trying to replace a certain div element parent with another one newparent. I want to copy only some of parent‘s children and put them in newparent, and then replace the parent by newparent.
Here is a snippet of my code:

var sb_button = parent.firstChild;
    var temp;
    while(sb_button) {
        console.log("loop: ");
        console.log(sb_button.id);
        temp = sb_button;
        if(sb_button.id != curr_button.id && sb_button.id != prev_button.id) {
            console.log("if");
            newparent.appendChild(temp);
            }
        else if(sb_button.id == curr_button.id) {
            console.log("elseif");
            newparent.appendChild(temp);
            newparent.appendChild(prev_button);
            }
        else {
            console.log("else");
            }
        sb_button.parentNode = parent;
        console.log(sb_button.id)
        console.log(sb_button.parentNode.children);
        sb_button = sb_button.nextSibling;
        }
    parent.parentNode.replaceChild(newparent,parent);

EDIT :

So when I do newparent.appendChild(temp) it modifies sb_button. What’s the workaround for this?

  • 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-23T02:44:15+00:00Added an answer on May 23, 2026 at 2:44 am

    I haven’t run your code, but there’s a few weird things, perhaps one of which may cause the issue or help clear up the code so the issue is more obvious.

    • the variable temp seems to be an alias for sb_button: you could remove the variable declaration and replace all references with temp
    • sb_button is a confusing name for an arbitrary child node
    • you’re appending the node in sb_button to newparent within the if statement, but right after you’re trying to set sb_button_.parentNode to parent – that’s not possible since parentNode is readonly and it certainly doesn’t make sense – you can’t append the element to one element but have a different parent.
    • are you trying to copy or move nodes?

    Edit: given that you want to copy nodes, I believe you’re looking for cloneNode: make a copy of the node and append that copy, not the original node.

    As a matter of clean design, when things get complicated, I’d avoid this kind of hard-to-reason-about while loop. Instead, simply make an array of the nodes, order those the way you want (you could even do this using sort to make it immediately obvious you’re just rearranging things), and then make a function that takes a newparent and the array and appends copies of all elements in array order to newparent. Your example isn’t that complex, but even here, I’d change the order of the if-clauses to have the “default” case in the final else. e.g.:

    for(var child = parent.firstChild; child; child = child.nextSibling) 
        if(child.id == curr_button.id) { //insert prev_button after curr_button
            newparent.appendChild(child.cloneNode(true));
            newparent.appendChild(prev_button.cloneNode(true));
        } else if(child.id != prev_button.id) {
            newparent.appendChild(child.cloneNode(true));
        }
    parent.parentNode.replaceChild(newparent, parent);
    

    The idea being to make it instantly obvious to the reader that all children are processed exactly once.

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

Sidebar

Related Questions

I'm trying to replace certain text inside the div info when an image (which
I'm trying to find some certain blocks in my data file and replace something
I am trying to replace certain functions using SED. Let say I have some
I'm trying to replace only certain parts of a string via an SQL query.
I am trying to replace a certain character in a string with a space
I am trying to replace a certain word in php and include a whitespace
I'm trying to completely replace a fragment back stack with one I generate based
I'm trying to extract only certain elements of a string using regular expressions and
I am trying to create a custom View that would replace a certain layout
I am trying to make an extension as part of which i want certain

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.