Solved

Jquery change order of divs

Posted on 2012-12-21
5
346 Views
Last Modified: 2012-12-21
Hello experts.
I need some help to finish my code.
I want to change the order of my divs with my goup and godown functions.
Not the ajax function.
I did test this tutorial http://biostall.com/swap-and-re-order-divs-smoothly-using-jquery-swapsie-plugin but it is not what i was looking for because it is not really changing the order but adding a style to display the divs in different positions.
Maybe the insertAfter() can help but i'm not familiar with this.

Any help?
<div id="container">
<!-- First DIV-->
<div id="item_04446AE9-D82B-EC26-AA89CF89A88BBB3C" class="clearboth">
 <div></div>
 <div></div>
 <div></div>
 <div id="sub_04446AE9-D82B-EC26-AA89CF89A88BBB3C" class="clearboth" style="width:100%">
  <div></div>
  <div align="right" class="flright inline"style="width:50%">
   <!-- IF THIS IS FIRST DIV HIDE --->
   <a href="#" onclick="goup('04446AE9-D82B-EC26-AA89CF89A88BBB3C');return false;" title="Go up" style="display:none;" ><img src="img/arrowup.gif" alt="Go up" border="0"></a>
   <!-- IF THIS IS LAST DIV HIDE --->
   <a href="#" onclick="godown('04446AE9-D82B-EC26-AA89CF89A88BBB3C');return false;" title="Go down" ><img src="img/arrowup.gif" alt="Go Down" border="0"></a>
  </div>
 </div>
  <hr class="clearboth"><br /><br />
</div>
<!-- Second div -->
<div id="item_057E81DC-B73B-7CFE-D409ED4FDAE24C49" class="clearboth">
 <div></div>
 <div></div>
 <div></div>
 <div id="sub_057E81DC-B73B-7CFE-D409ED4FDAE24C49" class="clearboth" style="width:100%">
  <div></div>
  <div align="right" class="flright inline"style="width:50%">
  <!-- IF THIS IS FIRST DIV HIDE --->
   <a href="#" onclick="goup('057E81DC-B73B-7CFE-D409ED4FDAE24C49');return false;" title="Go up" ><img src="img/arrowup.gif" alt="Go up" border="0"></a>
   <!-- IF THIS IS LAST DIV HIDE --->
   <a href="#" onclick="godown('04446AE9-D82B-EC26-AA89CF89A88BBB3C');return false;" title="Go down" style="display:none;"  ><img src="img/arrowup.gif" alt="Go Down" border="0"></a>
  </div>
 </div>
  <hr class="clearboth"><br /><br />
</div>
</div>
<script type="text/javascript">
var godown = function (f){
	var id = f ;
	//ajax function to change order in database. If success Gon on
	//current div
	var div1 = $('#item_' + f);
	//i have to find the raget div
	var divnext = div1.next().attr("id");
	alert(divnext)
    // function to change order and to check if it is first or last to hide - show the atags
			}
var goup = function (f){
	var id = f ;
	//ajax function to change order in database. If success Gon on
	//current div
	var div1 = $('#item_' + f);
	//i have to find the raget div
	var divprev = div1.prev().attr("id");
	alert(divprev)
	// function to change order and to check if it is first or last to hide - show the atags
   
			}
</script>

Open in new window

0
Comment
Question by:Panos
  • 3
  • 2
5 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 38713736
Here's a simple example:

<!DOCTYPE html>
<html>
  <head>
    <script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
  </head>
  <body>
    <div id="div1">This is div 1</div>
    <div id="div2">This is div 2</div>
    <input type="button" onclick="doMove();" value="Move"></input>
    <script>
      function doMove() {
        $('#div2').detach().insertBefore('#div1');
      }
    </script>
  </body>
</html>

Open in new window

0
 
LVL 2

Author Comment

by:Panos
ID: 38713788
Hi kaufmed
It is working.
The only problem is now to show or hide the a tags if the new position is first or last.
Any idea how?
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 500 total points
ID: 38714101
You can try something like:

function doMove() {
  var insertedDiv = $('#div2').detach().insertBefore('#div1');
  var parentsFirstChild = insertedDiv.parent().children(':first');

  if (insertedDiv.is(parentsFirstChild)) {
    insertedDiv.children('a').hide();
  }
}

Open in new window

0
 
LVL 2

Author Comment

by:Panos
ID: 38714185
Very good.
Thank you for your help
0
 
LVL 2

Author Closing Comment

by:Panos
ID: 38714187
Thank you
regards
panos
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

by Julian Matz As of jQuery (http://jquery.com/) 1.4 the .live() method (http://api.jquery.com/live/) supports custom events as well as some standard JavaScript events that it previously didn't support. Among these is the submit event. Unlike …
Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
The viewer will learn how to dynamically set the form action using jQuery.
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)

773 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