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

  • SEARCH
  • Home
  • 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 6118913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:29:29+00:00 2026-05-23T15:29:29+00:00

I have the following html <div id=to-sort> <div class=block data-id=1>aaaaa</div> <div class=block data-id=2>bbbbb</div> <div

  • 0

I have the following html

<div id="to-sort">
    <div class="block" data-id="1">aaaaa</div>
    <div class="block" data-id="2">bbbbb</div>
    <div class="block" data-id="3">ccccc</div>
    <div class="block" data-id="4">ddddd</div>
    <div class="block" data-id="5">eeeee</div>
    <div class="block" data-id="6">fffff</div>
    <div class="block" data-id="7">ggggg</div>
    <div class="block" data-id="8">hhhhh</div>
    <div class="block" data-id="9">iiiii</div>
</div>

I want to iterate through each .block and set its position to be absolute. I have the following

// Iterate through .blocks
$('.block').each(function(i,v){
    var cssobj = { position: 'absolute', 
                   top: $(this).position().top, 
                   left: $(this).position().left };

    console.log(cssobj);
    //$(this).css(cssobj);
});

If the line

//$(this).css(cssobj); 

is commented out – console.log() shows the correct positions. If I uncomment

$(this).css(cssobj); 

then they are all set to the position of the first .block (x:0, y:0)

I am sure this to do with scope but I tried a closure and couldnt get it to work. How do i set each div to be an absolute position?

Thanks

  • 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-23T15:29:30+00:00Added an answer on May 23, 2026 at 3:29 pm

    jQuery .position() gets the offset relative to the offset parent: http://api.jquery.com/position/

    jQuery .offset() is what you are looking for.

    Also after the first one sets itself to absolute the next one moves up in its place.

    instead put it in two passes:

    $('.block').each(function(){
        var cssobj = {top: $(this).offset().top, left: $(this).offset().left };
        $(this).css(cssobj);
    }).each(function(){ $(this).css('position', 'absolute')});
    

    EDIT:-

    as an alternative you can do it in reverse order and that will work also:

    $.fn.reverse = [].reverse;
    $('.block').reverse().each(function(i,v){
        var cssobj = {position: 'absolute',
                      top: $(this).offset().top, 
                       left: $(this).offset().left };
    
        console.log(cssobj);
        $(this).css(cssobj);
    });
    

    .reverse() is from JQuery .each() backwards

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

Sidebar

Related Questions

I have following HTML <div id=finalTree> <ul> <li class=last style=display: list-item;> <a id=DataSheets href=#>Data
I have the following html <div id=menu> <ul class=horizMenu> <li id=active><a href=# id=current>About</a></li> <li><a
I have the following HTML <div id=testID class=test1> <img id=testID2 class=test2 alt= src=some-image.gif />
i have following HTML structure: <div class=container> <div class=sidebar></div> <div class=content></div> <div class=subscript></div> </div>
I have following html: <div class=rows> <div class=row><input type=text onchange=javascript:getIndex(this); value= /></div> <div class=row><input
I have the following html code: <div class=outer ui-draggable style=position: relative;> <div class=inner>Foo bar</div>
I have the following html: <div class=bf_form_row> <label for=findout>Text goes here</label> <textarea class=findOut cols=40
I have following html: <div id=nav> <ul> <li class=keyitem>keyitem 1</li> <li>item</li> <li>item</li> <li>item</li> <li
I have the following html code: <div id=result1 class=result> ... some html ... ...
I have HTML like the following on my website: <div class=groups> <div class=group> Group

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.