0

I have two divs as follows:

HTML:

<body>
<div id="container">
    <div id="health"></div>
    <div id="food"></div>
    <button id="click" onClick="position();">sdcasdfzsdvv</button>
</div>
</body>

CSS :

@charset "utf-8";
*{
    padding:0px;
    margin:0px;}

body{
    background-color:#F4F4F2;
    margin-top:15px;}

#container{
    width:270px;
    height:162px;
    margin-top:0px;
    margin-left:auto;
    margin-right:auto;
    position:fix;}

#health{
    height:80px;
    margin-bottom:0px;
    border: solid 1px red;
    background-color:green;}

#food{
    height:80px;
    margin-bottom:0px;
    border: solid 1px blue;
    background-color:yellow;}

button{
    width:100px;
    height:80px;}

I want to have a function in JavaScript to move these two divs in a way that once the yellow on top and green bottom and by clicking on the button, the green moves up and yellow moves down and vice versa.

but in the first trial, the code below does not work. I don't know how to change the position of these divs without changing their positions to absolute and relative and without using jQuery. I'd be grateful if you kindly help.

function position() {
    document.getElementById('health').style.top='80px';
    document.getElementById('food').style.top='0px';
}
2
  • can you explain why #container has position:fix;? which is also wrong it s fixed not fix. Commented Oct 6, 2014 at 17:00
  • @ Onur TOPAL, because every time I tried different .style.top in the function, the container would jump. So I thought I have to fix its position. Commented Oct 6, 2014 at 17:04

6 Answers 6

1

You must add position:absolute to the divs.
You must add " before and after the number in the Javascript.

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

Comments

1

use JS only:

<body>
<div id="container">
    <div id="health" class="top-div"></div>
    <div id="food" class="bottom-div"></div>
    <button id="click" onClick="position()">sdcasdfzsdvv</button>
</div>

<script>
function position() {
    var topdiv = document.getElementsByClassName("top-div")[0];
    var bottomdiv = document.getElementsByClassName("bottom-div")[0];
    topdiv.parentNode.insertBefore(bottomdiv,topdiv);
    topdiv.className = 'bottom-div';
    bottomdiv.className = 'top-div';
}
</script>

</body>

fiddle: http://jsfiddle.net/rpopanrp/

Comments

0

Try:

function position() {
    document.getElementById('health').style.marginTop="80px";
    document.getElementById('food').style.marginTop="0px";
}

1 Comment

sorry I just had pasted an incomplete code - I edited my question. That won't solve the problem.
0

try below code there are also explanation with the changes.

#container{
    width:270px;
    height:162px;
    margin-top:0px;
    margin-left:auto;
    margin-right:auto;
    position:relative; //says the child will be relative to that not body
}

#health{
    height:80px;
    border: solid 1px red;
    background-color:green;
    position:absolute; // the positining for that by the top and left not flow position defalt is top:0 left 0
}

function position() {
    document.getElementById('health').style.top="80px";
}

just implement what you want to other div. this should work.

2 Comments

also it is better if you give width to the elements.
@TOPAL yes, when I change their position to absolute, I changed the width
0

Would this work for you? If not, try to be more specific. FIDDLE

function position() {
    var container = document.getElementById("container");
    var divs = container.getElementsByTagName("div");
    container.insertBefore(divs[1], divs[0]);
}

3 Comments

The function you have written is right, I just checked it but it does not work. The function should swap the position of divs health and food.
Have you checked the fiddle? You didn't position your divs using absolute positioning, so you don't have to modify top property of the divs. Changing their html order swaps their position.
@aknuri Thanks I checked and it worked - Thanks again
0

http://jsfiddle.net/45zkgk57/6/

JS:

    click=1;
function position() {
    if(click==1) {
    document.getElementById('health').style.top='80px';
    document.getElementById('food').style.top='0px';
        click=2;
    }

    else {
            document.getElementById('health').style.top='0px';
    document.getElementById('food').style.top='80px';
        click=1;
    }
}

Also, css is little changed, check fiddle. (Set position: absolute to elements)

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.