Solved

Jquery change order of divs

Posted on 2012-12-21
5
367 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

710 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