re-order divs with javascript?

Status
Not open for further replies.

Stray Toaster

Ars Tribunus Militum
2,292
Subscriptor++
I am re-designing my webpage currently, and I have it set up to have a left-hand sidebar navigation, with main content to the right. I also have it set up, so the stuff on the right can scroll horizontally and in effect goes under the left-hand nav. The left hand stuff is fixed so doesn't move when scrolling.

Now, everything works fine, but the order of the divs in my HTML is backward how I would prefer it.

Currently its like this:

Code:
<!-- Main content area -->
<div class=Content>
  <div class=Images>image</div>
  <div class=Images>image</div>
  <div class=Images>image</div>
  <div class=Images>image</div>
</div>
<!-- Left Hand Nav -->
<div class=Container>
  <div class=side bar>
    <div class=nav>stuff</div>
  </div>
</div>

However, for my own internal logic, I would prefer the left hand nav to come first in the HTML document, but this causes the main content area to go over it and not under it.

Is there a simple javascript snippet I can put in to re-order the divs, so I can have the left hand stuff first? Or am I just being unnecessarily picky?
 
Something like this?

Code:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" charset="utf-8">
function switchdivs() {
    var first  = document.getElementById("first").innerHTML;
    var second  = document.getElementById("second").innerHTML;

    var t = first;
    document.getElementById("first").innerHTML = second;
    document.getElementById("second").innerHTML  = t;
}
</script>
</head>

<body onload="switchdivs()">
<div id="first"><p>first</p></div>
<div id="second"><p>second</p></div>
</body>
</html>
 

technophile

Ars Legatus Legionis
23,768
Forget the JS, just rewrite it like this:

Code:
<html>
    <head>
        <style type="text/css">
            .right_container
            {
                width: 100%;
                margin-left: -250px;
                float: right;
            }
            
            .right
            {
                margin-left: 250px;
            }
            
            .left
            {
                width: 225px;
                float: left;
            }
        </style>
    </head>
    <body>
        <div class="right_container">
            <div class="right">
                This is the main content area.
            </div>
        </div>
        <div class="left">
            This is the left nav area.
        </div>
    </body>
</html>
 
Status
Not open for further replies.