Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6142239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:21:36+00:00 2026-05-23T18:21:36+00:00

$(document).ready(function() { $(‘div.post’).click(function() { // the clicked LI var clicked = $(this); // all

  • 0
$(document).ready(function() {
$('div.post').click(function() {
  // the clicked LI
  var clicked = $(this);

  // all the LIs above the clicked one
  var previousAll = clicked.prevAll();

  // only proceed if it's not already on top (no previous siblings)
  if(previousAll.length > 0) {
    // top LI
    var top = $(previousAll[previousAll.length - 1]);

    // immediately previous LI
    var previous = $(previousAll[0]);

    // how far up do we need to move the clicked LI?
    var moveUp = clicked.attr('offsetTop') - top.attr('offsetTop');

    // how far down do we need to move the previous siblings?
    var moveDown = (clicked.offset().top + clicked.outerHeight()) - (previous.offset().top + previous.outerHeight());

    // let's move stuff
    clicked.css('position', 'relative');
    previousAll.css('position', 'relative');
    clicked.animate({'top': -moveUp});
    previousAll.animate({'top': moveDown}, {complete: function() {
      // rearrange the DOM and restore positioning when we're done moving
      clicked.parent().prepend(clicked);
      clicked.css({'position': 'static', 'top': 0});
      previousAll.css({'position': 'static', 'top': 0}); 
    }});
  }
});

});

How can I move a div to the top of a list of divs upon clicking a link.

eg;

<div id=1>Div One <a>Click to update</a><a>a different link</a></div>
<div id=2>Div One <a>Click to update</a><a>a different link</a></div>
<div id=3>Div One <a>Click to update</a><a>a different link</a></div>
<div id=4>Div One <a>Click to update</a><a>a different link</a></div>
<div id=5>Div One <a>Click to update</a><a>a different link</a></div>

and when you click ONLY on the link “CLICK TO UPDATE” for any div, it should move that div to the top of the page!


80% done. Thanx guys for the swift response. Preciate it to the max. Anyways thanx to @valipour I managed to get the div to move ONLY when you click on a specific link by adding a class to the link and changing my first two lines from;

$('div.post').click(function() {
// the clicked LI
  var clicked = $(this);

to;

$("a.rep").click(function() {
    var clicked = $(this).closest("div.post");

html code is;

<div id="wrapper">
<div class="post"><a class="rep">1 Aasdjfa</a> <a>6 Aasdjfa</a></div>
<div class="post"><a class="rep">2 Aasdjfa</a> <a>7 Aasdjfa</a></div>
<div class="post"><a class="rep">3 Aasdjfa</a> <a>8 Aasdjfa</a></div>
<div class="post"><a class="rep">4 Aasdjfa</a> <a>9 Aasdjfa</a></div>
<div class="post"><a class="rep">5 Aasdjfa</a> <a>10 Aasdjfa</a></div>
</div>

Thanks!

BUT it doesn’t work with div’s that were dynamically loaded? eg. I have 10 divs showing by default then I have a script that loads more divs when you scroll…this script won’t move the divs in the autoload section when you click on any of them…any idea why???

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T18:21:38+00:00Added an answer on May 23, 2026 at 6:21 pm

    If you put them all inside another wrapper div you could do
    (This is now the most upto date version):

    $("#wrapper a.rep").live('click', function(){
      $(this).parents('.post').hide().prependTo("#wrapper").slideDown();
    });
    

    EDITED my answer. This one works. With animation aswell 😉.

    If you dont like animation, have just $(this).parent().prependTo(“#wrapper”) **

    http://jsfiddle.net/2DjXW/18/

    To load Divs that are dynamically added afterwards, you will have to use the ‘live’. When document is ready the divs that are not there cannot have events added. But live adds the events when new are dynamically added.

    New edit:
    http://jsfiddle.net/tVhqz/8/

    Should work now well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

$(document).ready(function(){ $('a[rel=addhint]').click(function(){ var html='<div><label>Hint</label>'; html+='<span class=input><input type=text size=30 class=text name=hint/><a href=# rel=delhint>delete</a></span><br class=clear /></div>';
$(document).ready(function() { $('a.menuitem').click(function() { var arr = 0; var link = $( this ),
$(document).ready(function(){ $('.choices a img').click(function(e) { var url = $(this).attr('href') + ' .con_cen2'; $('.con_cen').html('loading...').load('signup.html'); e.preventDefault();
$(document).ready(function() { $(.po).click(function(){ var po = $(this).text(); var dataString = 'po='+ po; $.ajax ({
$(document).ready(function() { $('#comment textarea').click(function() { if (this.value == this.'Post a comment') { this.value =
$(document).ready(function() { function countChecked() { var n = $(input:checked).length; $(div).text(n + (n <= 1
$(document).ready(function() { $('#username').focus(function() { $(this).val(''); }) $('#username').blur(function() { var l = $(this).val.length; if (l
$(document).ready(function(){ $('#hideButton').click(function(){ $('#disclaimer,#clock').hide(); }); }); In the above code snippet, suppose I want to
$(document).ready(function() { $(form#login_form).submit(function() { var login_username = $('#login_username').attr('value'); var login_password = $('#login_password').attr('value'); type: POST,
$(document).ready(function() { function GetDeals() { alert($(this).attr(id)); } $('.filterResult').live(click, function(event) { GetDeals(); }); }); What

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.