2

I'm a bit unsure of how to do this. I have a main panel where I want to display a DIV in that based on an action. I have the DIVS laid out like this in the html:

<div id="container">
   <div id="1"></div>
   <div id="2"></div>
</div>

Rather than messing around with positioning, I want to make it so that when something happens I can just put div id=2 above div id=1, as simply as possible. Any advice is appreciated.

1
  • So when you said above did you mean the z-index, or above in sequential layout order? Commented Jan 10, 2011 at 17:38

3 Answers 3

6

In the example you are showing you can simply use the append, and prepend jquery methods.

For example, div#1 then div#2

$('#container').append('#1').append('#2');

Reorder, div#2 then div#1

$('#container #2').remove().prependTo('#container');

There are many ways you could write this. Just remember prepend places an element at the beginning (before the first element) of the container, while append places the element after the last element.

Sign up to request clarification or add additional context in comments.

1 Comment

@torazaburo - The question was tagged with jQuery by the OP. This answer was accepted as the correct answer by the OP. This question is also over 2 years old. Since you don't seem to understand down-voting read up here.
1

You simply do this absolute positioning the divs. If something happens, just set z-index of the elements. For example (using simple JS):

var d1 = document.getElementById('1');
var d2 = document.getElementById('2');

d1.style.zIndex = 1;
d2.style.zIndex = 2;

If you want to update them, just reset the zIndex ;)

4 Comments

ah ok so they can just be on top of each other then.. I'm kinda rusty on my CSS so didn't realize this
Yes! With absolute positioning sure! Or maybe if you just want to show/hide the divs you can use d1.style.display = "none" and/or d2.style.display = "block". If you just want to switch the DOM elements, you can use some jQuery methods to change their order! (like Josiah Ruddell wrote)
ahh I don't think I can use absolute positioning for what I'm trying to do, will have to try the other way of doing it
Ok, np, you can easily switch the elements with jQuery :) ..OR.. if you want to test and/or try another framework (i.e. mootools) look at this great example by david walsh: davidwalsh.name/dw-content/mootools-swap.php
0

remove the second element and reinsert it before the first element.

javascript:

var d1 = document.getElementById('1');
var d2 = document.getElementById('2');

d2.parentNode.removeChild(d2);
d1.parentNode.insertBefore(d2, d1);
​

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.