Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi There are multiple <div>tags in my page, i just want to move position of a <div> up when UP button is clicked.for example id the position of the div is 10 from top when UP button is clicked its position should be 9th and the 9th div should appear at the 10th position.similarly for DOWN button.Can anyone help me
Posted
Comments
Muralikrishna8811 17-Oct-11 9:06am    
hi you wanna develop that feature

you've to maintain database table for that.
means div position details.He can add div tag or place div tags in positions.

because that changes must effects from server in that website.
Muralikrishna8811 18-Oct-11 11:51am    
hi ,

I developed one function like in that website.

if it works for you then i'll make remaining.
Muralikrishna8811 18-Oct-11 11:53am    
try in IE

k

Hi,

check this once

HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
    <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    <script language="javascript">

        function moveup(tid) {

            var len = document.getElementById("maindiv").childNodes.length;
            var w;
            for (var y = 0; y < parseInt(len); y++) {
                if (document.getElementById("maindiv").childNodes[y].innerHTML == document.getElementById(tid).innerHTML) {
                    w = y;
                }
            }
            chang(w);

        }
        function chang(g) {
            if (parseInt(g) > 1) {
                var fg = document.getElementById("maindiv").childNodes[g].innerHTML;
                document.getElementById("maindiv").childNodes[g].innerHTML = document.getElementById("maindiv").childNodes[parseInt(g) - 2].innerHTML;
                document.getElementById("maindiv").childNodes[parseInt(g) - 2].innerHTML = fg;
            }
        }
    
    </script>
    <style type="text/css">
        #maindiv
        {
            height: 497px;
        }
    </style>
</head>
<body >
    <form id="form1" runat="server">
   <div id="maindiv">
      <div id="Div1" style=" background-color :Black; color :Red; width:400px; height :100px;">
        <input type ="button" id="btnup" value ="UP" onclick="moveup(this.parentNode.id)"  />

        <br />
        This is first content
     </div><br />
     <div id="Div2" style=" background-color :Black; color :Red; width:400px; height :100px;">
       <input type ="button" id="Button1" value ="UP" onclick="moveup(this.parentNode.id)"  />

       <br />
        This is second content
     </div><br />
     <div id="Div3" style=" background-color :Black; color :Red; width:400px; height :100px;">
       <input type ="button" id="Button2" value ="UP" onclick="moveup(this.parentNode.id)"  />

       <br />
       This is third content
     </div><br />
     <div id="Div4" style=" background-color :Black; color :Red; width:400px; height :100px;">
       <input type ="button" id="Button3" value ="UP" onclick="moveup(this.parentNode.id)"  />

       <br />
       This is fourth content
     </div>
   </div>
   <br />
 

    </form>
</body>
</html>


All the Best
 
Share this answer
 
Comments
radhika 5 19-Oct-11 5:18am    
Hi Murali

How can we get the id of the element above the element whose id is known?
Muralikrishna8811 19-Oct-11 5:21am    
that means parent id

i'm trying in another way don't worry abt it
radhika 5 19-Oct-11 5:49am    
OK
For parent id u have given "this.parent.id" i got this.
But how to get the Id of the element which is above the known element when both elements are at same level?

radhika 5 19-Oct-11 6:36am    
Hi murali i got it.

i just changed childNodes[parseInt(g) - 2] to childNodes[parseInt(g) - 3]
radhika 5 19-Oct-11 6:37am    
Thank you soo much
Hi,

just check this once for your requirement.

place your cursor in textbox then press buttons up and down you'll see your required output.

HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
    <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>

    <script language ="javascript" >
        function f1() {
//            $("#resdiv").append("<span onmouseenter="f2(this)" onmouseout="f3(this)" style="width:318px;">hello</span><br /><span onmouseenter="f2(this)" onmouseout="f3(this)" style="width:318px;">sdfsdfsdfsdf</span><br /><span onmouseenter="f2(this)" onmouseout="f3(this)" style="width:318px;">sdfsdf</span>");
        }
        function f2(tid) {
            
            tid.style.backgroundColor = "White";
            tid.style.color = "red";
        }
        function f3(tid) {
            tid.style.backgroundColor = "";
            tid.style.color = "";
        }
        var tmp = -1;
        function f4() {
            var len = document.getElementById("parntdiv").childNodes.length;

           
            if (parseInt(event.keyCode) == 40) {
                //down
                if (parseInt(tmp) ==parseInt(len)-1) {
                    tmp = 0;
                }
                else {
                    
                        tmp = parseInt(tmp) + 1;
                    
                }
              
             
            }
            if (parseInt(event.keyCode) == 38) {
                //up

                if (parseInt(tmp) <= 0) {
                    tmp = parseInt(len) - 1;
                }
                else {
                    tmp = parseInt(tmp) - 1;
                }

            }
            fq();
        }
        function fq() {
            var len = document.getElementById("parntdiv").childNodes.length;
           
            if (parseInt(tmp) != parseInt(len) - 1) {
                f3(document.getElementById("parntdiv").childNodes[parseInt(tmp) + 1]);
            }
            if (parseInt(tmp) == parseInt(len) - 1) {
                f3(document.getElementById("parntdiv").childNodes[0]);
            }

            f2(document.getElementById("parntdiv").childNodes[parseInt(tmp)]);
            document.getElementById("txtsearch").innerText = document.getElementById("parntdiv").childNodes[parseInt(tmp)].innerText;
            if (parseInt(tmp) > 0) {
                f3(document.getElementById("parntdiv").childNodes[parseInt(tmp) - 1]);
            }
            if (parseInt(tmp) == 0 || parseInt(tmp) == -1) {
                f3(document.getElementById("parntdiv").childNodes[parseInt(len) - 1]);
            }
            
        }
    </script>
  
    <style type="text/css">
        #txtsearch
        {
            width: 318px;
        }
    </style>
  
</head>
<body>
    <form id="form1" runat="server">
     <div id="dfsdf">
   
       <input type ="text" id="txtsearch" onkeyup="f1()" onkeydown="f4()"   /><br />
       
        <div id="parntdiv" style=" width:318px; background-color :Black ; color:White;">
          <div id="child1" onmouseenter="f2(this)" onmouseout="f3(this)">child one</div>
           <div id="child2" onmouseenter="f2(this)" onmouseout="f3(this)">child two</div>
            <div id="child3" onmouseenter="f2(this)" onmouseout="f3(this)">child three</div>
             <div id="child4" onmouseenter="f2(this)" onmouseout="f3(this)">child four</div>
             <div id="child5" onmouseenter="f2(this)" onmouseout="f3(this)">child five</div>
             <div id="child6" onmouseenter="f2(this)" onmouseout="f3(this)">child six</div>
        </div>
        <br />
        <div id="resdiv" style=" width:318px; background-color :Black ; color:White;"></div>
     </div>
    </form>
</body>
</html>

In the above code you can set those events for normal up and down keystrokes.

I hope you'll understood what I did.

All the Best
 
Share this answer
 
Comments
radhika 5 17-Oct-11 5:45am    
Hi Murali,
Thanks for the reply.The above code does'nt match my requirement.See i have 2 buttons Up arrow and Down arrow for each div tag.For example when i click on up arrow of the child6 div tag then the child6 shoulh go up and the child5 div should come down.If i click again the up arrow of the child6 div tag then it should move to the child4 position and child4 should come down.Same thing when i click down the div tag should come down.Please try to send asap.


See this code is for moving the div tag up and down,


<HTML>
<HEAD>
<TITLE>Document Title</TITLE>
</HEAD>
<BODY>
<SCRIPT language="JavaScript">

var DHTML = (document.getElementById || document.all || document.layers);

function getObj(name){

if (document.getElementById){
this.obj = document.getElementById(name);
this.style = document.getElementById(name).style;
}
else if (document.all){
this.obj = document.all[name];
this.style = document.all[name].style;
}

else if (document.layers){
this.obj = document.layers[name];
this.style = document.layers[name];
}
}

var SwitchD =294;

moved_up=0
moved_down=0

function move(amount,dir){
if (!DHTML) return;
if(moved_up==0&&dir=="down"){
var x = new getObj('ContentMain');
SwitchD += amount;
x.style.top = SwitchD;
moved_up=1;
moved_down=0;
}

if(moved_down==0&&dir=="up"){
var x = new getObj('ContentMain');
SwitchD += amount;
x.style.top = SwitchD;
moved_down=1;
moved_up=0;
}

}
//-->
</SCRIPT>


<Body>
Down
Up

<div id="ContentMain" style="position:absolute; left:115px; top:294px; z-index:30; width: 550px; height: 274px; overflow: visible;background-color:blue"></div>


</BODY>
</HTML>

i need when multiple div tags are present in the page then when we move a tag up the tag above should come down and vice versa

Thanks in advance
Muralikrishna8811 17-Oct-11 5:47am    
Great ,

soon i'll post
radhika 5 17-Oct-11 6:12am    
Hi Murali,
Could you please go through this site.

http://mysamplesite.page4.me

id: radhika.radhi67@gmail.com
password : 51897246

When you place the cursor in the content page of the body of the page you will find 2 arrows up and down.when u click up the content moves up.
Hi Murali,
Thanks for the reply.The above code does'nt match my requirement.See i have 2 buttons Up arrow and Down arrow for each div tag.For example when i click on up arrow of the child6 div tag then the child6 shoulh go up and the child5 div should come down.If i click again the up arrow of the child6 div tag then it should move to the child4 position and child4 should come down.Same thing when i click down the div tag should come down.Please try to send asap.Thanks in advance
 
Share this answer
 
Comments
Muralikrishna8811 17-Oct-11 5:36am    
K you saying tag sorting based on positions right.

And one more thing is you can rply through "have a question or comment" option

K soon i'll back with solution
Muralikrishna8811 17-Oct-11 6:38am    
Hi radhika check my solution now.

I hope It fulfill your requirement
Hi,

check this once

HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script language="javascript" >
        var crt;
        function f2(tid) {
            clrall();
            tid.style.backgroundColor = "White";
            tid.style.color = "red";
            crt = tid;
        }
        function clrall() {
            var len = document.getElementById("parntdiv").childNodes.length;
            for (var i = 0; i < parseInt(len); i++) {
                f3(document.getElementById("parntdiv").childNodes[i]);
            }
        }
        function f3(tid) {
            tid.style.backgroundColor = "";
            tid.style.color = "";
        }
        function chk(tid) {
            clrall();
            tid.style.backgroundColor = "White";
            tid.style.color = "red";
        }
        function updiv() {

            var len = document.getElementById("parntdiv").childNodes.length;
            var int;
            for (var i = 0; i < parseInt(len); i++) {
                if (crt.innerText == document.getElementById("parntdiv").childNodes[i].innerText) {
                    if (parseInt(i) == 0) {
                    }
                    else {
                        int = i;
                    }
                }

            }
            moveup(int);
        }
        function moveup(tid) {
            var pre, curt;

            curt = document.getElementById("parntdiv").childNodes[tid];

            pre = document.getElementById("parntdiv").childNodes[parseInt(tid) - 1].innerText;

          document.getElementById("parntdiv").childNodes[parseInt(tid) - 1].innerText = curt.innerText;
          document.getElementById("parntdiv").childNodes[tid].innerText = pre;
          f2(document.getElementById("parntdiv").childNodes[parseInt(tid) - 1]);
        }
        function downdiv() {
            var len = document.getElementById("parntdiv").childNodes.length;
            var int;
            for (var i = 0; i < parseInt(len); i++) {
               
                if (crt.innerText == document.getElementById("parntdiv").childNodes[i].innerText) {


                    int = i;
                    
                }

            }
            movedown(int);
        }
        function movedown(tid) {

            var ct = document.getElementById("parntdiv").childNodes[tid].innerText;
           
            document.getElementById("parntdiv").childNodes[tid].innerText = document.getElementById("parntdiv").childNodes[parseInt(tid) + 1].innerText;
            document.getElementById("parntdiv").childNodes[parseInt(tid) + 1].innerText = ct;
            f2(document.getElementById("parntdiv").childNodes[parseInt(tid) + 1]);
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input type ="button" value =" UP " onclick="updiv()" /><br />
        <input type="button" value =" DOWN " onclick ="downdiv()" /><br />
          <div id="parntdiv" style=" width:318px; background-color :Black ; color:White;">
          <div id="child1" onclick="f2(this)">child one</div>
           <div id="child2" onclick="f2(this)">child two</div>
            <div id="child3" onclick="f2(this)">child three</div>
             <div id="child4" onclick="f2(this)">child four</div>
             <div id="child5" onclick="f2(this)">child five</div>
             <div id="child6" onclick="f2(this)">child six</div>
        </div>
    </div>
    </form>
</body>
</html>


Some little modifications are required in the above code for full use.

All the Best
 
Share this answer
 
Comments
radhika 5 17-Oct-11 6:56am    
HI murali,
Its not working.
Muralikrishna8811 17-Oct-11 6:57am    
select one div among those divs then click down or up button
Muralikrishna8811 17-Oct-11 6:57am    
any error
Muralikrishna8811 17-Oct-11 7:06am    
check in IE
radhika 5 17-Oct-11 7:35am    
Its not working in IE too Murali

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900