Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Jquery change order of divs

Posted on 2012-12-21
5
Medium Priority
?
403 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 2000 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 2000 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

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…
There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
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)
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…

670 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