Solved

Jquery change order of divs

Posted on 2012-12-21
5
353 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Show/Hide ASP.NET controls with JQuery best practices 3 99
Create an automated page index 9 53
multiple selects 23 49
JS Event Does not Trigger From File 2 29
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
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)

821 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