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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:05:26+00:00 2026-06-15T02:05:26+00:00

I have the following DOM elements on one horizontal line: A div containing a

  • 0

I have the following DOM elements on one horizontal line:

  1. A div containing a line of text; i.e. “Hello world”. The width of this div is the same as the containing text.
  2. A button to swap the two divs.
  3. A div containing a listbox. The width of this div is the same as the containing listbox.

With float or position:absolute I can align these elements correctly on one line. However, when the user clicks the swap button, the divs should swap places. I would prefer not to remove the divs from the DOM and adding them again in each other’s place. That’s because the elements are actually Javascript objects (Google Closure Library components to be more specific) that hold variables and have event handlers attached to them. Is it possible to swap the divs using CSS, without assigning a static width to the divs?

Simplified example:

<div style="position:relative; height:60px;">
  <div style="float:left;">Our Team</div>
  <div style="float:left;"><button onclick="swap();">swap home/away</button></div>
  <div style="float:left;"><select><option>Opponent A</option><option>Opponent B</option></select></div>
</div>
<div style="clear:both;"></div>

When the user clicks the button, the first (our team) and the third (opponent) div should be visually swapped in the browser, preferably without removing the elements from the DOM.

  • 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-06-15T02:05:27+00:00Added an answer on June 15, 2026 at 2:05 am

    I would prefer not to remove the divs from the DOM and adding them again in each other’s place. That’s because the elements are actually Javascript objects (Google Closure Library components to be more specific) that hold variables and have event handlers attached to them.

    The best solution is to move the elements in the DOM instead. Doing this will preserve existing event handlers, which means your code should still work:

    http://jsfiddle.net/thirtydot/LGGW2/

    function swap() {
        var target1 = document.querySelector('#swapMe > :first-child');
        var target2 = document.querySelector('#swapMe > :last-child');
    
        var parent = target1.parentNode;
        parent.insertBefore(target1, null);
        parent.insertBefore(target2, parent.firstChild);
    }​
    

    I’m assuming you already have a reference to the elements you want to swap, so you can replace the document.querySelector lines with whatever is suitable.

    After writing this answer, I searched for a more generic “swap” function and found this, which looks good: Swap 2 html elements and preserve event listeners on them

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

Sidebar

Related Questions

If I have the following DOM elements <div class=item>content1</div> <div class=item>content2</div> how, using jQuery
Say I have the following DOM element in my piece of code: <div id=test>
I have the following function to remove a DOM element div, $('#emDiv').on(click, ':button[data-emp-del=true]', function
I have the following DOM structure of interest: <div id=portfolioItems> <div class=portfolioItemsLine> <div class=portfolioItem
I have the following DOM: <div class=qtip qtip-light qtip-active> <div class=qtip-wrapper> <div class=qtip-borderTop></div> <div
I have a simple DOM code like the following <div> <div> </div> </div> I
Say I have the following html: <div id=list> <span id=one>toto</span> <span id=two>titi</span> </div> and
I have the following jQuery: $.fn.dataTableExt.afnSortData['dom-text'] = function (oSettings, iColumn) { var aData =
I have the following z.js method to apply styles dynamically to DOM element like
I have a following structered DOM result, getting after a POST event: <root> <config>

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.