Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Dynamically change order of elements on Html page

Posted on 2006-11-20
1
2,929 Views
Last Modified: 2009-04-28
Is it possible to dynamically change the order  of elements on an HTML page.  In other words, using Javascript, I would like to take this:

<div id="Div1" style="background-color:#EEEEEE">Div1</div>
<div id="Div2" style="background-color:#DDDDDD">Div2</div>
<div id="Div3" style="background-color:#CCCCCC">Div3</div>

and turn it into this:

<div id="Div1" style="background-color:#EEEEEE">Div1</div>
<div id="Div3" style="background-color:#CCCCCC">Div3</div>
<div id="Div2" style="background-color:#DDDDDD">Div2</div>

Thanks in advance for the help.
0
Comment
Question by:pguerrant
1 Comment
 
LVL 18

Accepted Solution

by:
Morcalavin earned 50 total points
ID: 17979736
You could do something like below.  This will order div 1 before div 4

<script>
function reorder() {
      div1 = document.getElementById('Div1');
      div1.parentNode.insertBefore(div1, document.getElementById('Div4'));
      
}
</script>

<body>
<input type=button value=Reorder onclick="reorder()" />
<div id="Div1" style="background-color:#EEEEEE">Div1</div>
<div id="Div2" style="background-color:#DDDDDD">Div2</div>
<div id="Div3" style="background-color:#CCCCCC">Div3</div>
<div id="Div4" style="background-color:#BBBBBB">Div4</div>
</body>
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question