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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:45:44+00:00 2026-05-26T16:45:44+00:00

What the best way to switch two elements in DOM with jquery? <div id=switchme2>some

  • 0

What the best way to switch two elements in DOM with jquery?

<div id="switchme2">some other elements in here....</div>
<div class="some other elements"></div>
<div id="switchme1">some other elements in here....</div>

should end in:

<div id="switchme1">some other elements in here....</div>
<div class="some other elements"></div>
<div id="switchme2">some other elements in here....</div>
  • 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-26T16:45:45+00:00Added an answer on May 26, 2026 at 4:45 pm

    You’ll get people telling you to use jQuery’s before, after, and such, but beware, that can mess things up if there are text nodes on either side of either element (more on that below).

    Because of that (and for people not using jQuery), you can just use the DOM directly:

    function swapElements(elm1, elm2) {
        var parent1, next1,
            parent2, next2;
    
        parent1 = elm1.parentNode;
        next1   = elm1.nextSibling;
        parent2 = elm2.parentNode;
        next2   = elm2.nextSibling;
    
        parent1.insertBefore(elm2, next1);
        parent2.insertBefore(elm1, next2);
    }
    

    Note that it’s fine if the reference element (next1 or next2 above) is null; insertBefore handles that correctly (by adding at the end of the parent, like appendChild would).

    Usage combined with jQuery:

    swapElements($("#switchme1")[0], $("#switchme2")[0]);
    

    Live example:

    jQuery(function($) {
      
        function swapElements(elm1, elm2, elm3, elm4, elm5) {
            var parent1, next1,
                parent2, next2,
                parent3, next3,
                parent4, next4,
                parent5, next5;
    
            parent1 = elm1.parentNode;
            next1   = elm1.nextSibling;
            parent2 = elm2.parentNode;
            next2   = elm2.nextSibling;
            parent3 = elm3.parentNode;
            next3   = elm3.nextSibling;
            parent4 = elm4.parentNode;
            next4   = elm4.nextSibling;
            parent5 = elm5.parentNode;
            next5   = elm5.nextSibling;
    
            parent1.insertBefore(elm2, next1);
            parent2.insertBefore(elm3, next2);
            parent3.insertBefore(elm4, next3);
            parent4.insertBefore(elm5, next4);
            parent5.insertBefore(elm1, next5);
        }
    
        $("#btnSwitch").click(function() {
            swapElements($("#switchme1")[0], $("#switchme2")[0], $("#switchme3")[0], $("#switchme4")[0], $("#switchme5")[0]);
        });
      
    });
    first text node
    <div id="switchme1">this is <strong>switchme1</strong> with <strong>some other elements</strong> <em>in here<div>test</div></em></div>
    second text node
    <div id="switchme2">this is <strong>switchme2</strong> with <strong>some other elements</strong> <em>in here</em></div>
    <div id="switchme3">this is <strong>switchme3</strong> with <strong>some other elements</strong> <em>in here</em></div>
    <div id="switchme4">this is <strong>switchme4</strong> with <strong>some other elements</strong> <em>in here</em></div>
    <div id="switchme5">this is <strong>switchme5</strong> with <strong>some other elements</strong> <em>in here</em></div>
    third text node
    <input type="button" id="btnSwitch" value="Switch Em!">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

    More about jQuery’s before, after, and such: Because jQuery tends to give you access mostly to elements (which is what you want 95% of the time!), if the elements you want to swap have text nodes around them, using those methods will mess things up. (You also have to either remember which one goes in front or figure it out.) In contrast, the swapElements method above above works regardless of whether the elements are surrounded by other elements or text nodes, and you don’t have to remember or figure out which should go in front.

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

Sidebar

Related Questions

Basically I need some suggestions which is the best way to switch views in
I'm trying to decide the best way of having radiobuttons switch around the contents
What's the best way of changing some rows back color in DataGridView, when it's
What is the best way to easily switch from a test database to a
the best way to explain is with example so: this is the model public
The best way I can think of to ask this is by example... In
The best way of describing this is I have a table of people with
The best way to store images into MySQL is by storing the image location
What is the best way to verify/test that a text string is serialized to
What is the best way of creating an alphabetically sorted list in Python?

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.