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 9001351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:25:29+00:00 2026-06-16T00:25:29+00:00

Using jQuery 1.5.2, I’m giving a random position to the children of a div,

  • 0

Using jQuery 1.5.2, I’m giving a random position to the children of a div, the code works in Firefox, Chrome and Opera, but it doesn’t work in IE7-9 and Safari. What is wrong?

A second question, when the page is refresed multiple times, the random pattern is not very random, sometimes the same number stays in first place, etc. Any hint to increase the randomness of the script?

PD: the jquery fiddle is 1.6.4

http://jsfiddle.net/6cqtP/

SCRIPTS

jQuery(document).ready(function() {
              jQuery('#postscript-top-inner').each(function(){
            var parent = jQuery(this);
            var content = parent.find('.block-nodeblock');

            content.sort(function(a){
                  var new_position = parseInt( Math.random()*4 );
                  return( new_position );
            })
            .appendTo(parent);       

      });
});

HTML

<div id="postscript-top-inner">
 <div class="block-nodeblock">
1
 </div>
  <div class="block-nodeblock">
2
 </div>
  <div class="block-nodeblock">
3
 </div>
  <div class="block-nodeblock">
4
 </div>
  <div class="block-nodeblock">
5
 </div>
  <div class="block-nodeblock">
6
 </div>
  <div class="block-nodeblock">
7
 </div>
  <div class="block-nodeblock">
8
 </div>
  <div class="block-nodeblock">
9
 </div>
  <div class="block-nodeblock">
10
 </div>
  <div class="block-nodeblock">
11
 </div>
  <div class="block-nodeblock">
12
 </div>
  <div class="block-nodeblock">
13
 </div>
  <div class="block-nodeblock">
14
 </div>
  <div class="block-nodeblock">
15
 </div>
  <div class="block-nodeblock">
16
 </div>

</div>

CSS

#postscript-top-inner{width: 460px; margin:0 auto}
.block-nodeblock{width:50px; padding:25px 0; background: gray; margin:0 10px 10px 0; text-align:center; float: left}
  • 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-06-16T00:25:30+00:00Added an answer on June 16, 2026 at 12:25 am

    You could replace your compareFunction with

    function() {
        return 0.5 - Math.random();
    }
    

    The Array.sort function sorts an array by means of the passed compareFunction(a, b). This works as follows

    If compareFunction(a, b) is less than 0, sort a to a lower index than
    b.

    If compareFunction(a, b) returns 0, leave a and b unchanged with
    respect to each other, but sorted with respect to all different
    elements.

    If compareFunction(a, b) is greater than 0, sort b to a lower index
    than a.

    Your compare function always returns values >= 0. It says that A >= B and B >= A. Depending on the random values, it mostly occurs that A > B. Accordingly, it would be impossible that B >= A. Due to this paradox, some browser implementations of Array.sort do not sort your array at all.

    To further improve the randomness, you could implement the Fisher-Yates algorithm.

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

Sidebar

Related Questions

Using jQuery I'd like to add additional top-margin to a div 'foo', but only
Using jquery.load() to replace the content of a div everything works great, however there
using jquery's .post i send do my php code an object that with var_dump
Using jQuery I'm trying to obtain the rotation state of an object but in
Using jQuery Ajax to fetch data from local server: it works well with IE8
Using jQuery 1.5.1 Tried this block of code in: head section as well as
Using JQuery: Sometimes, I can do variable.val() and it works. Sometimes, I'm required to
Using jQuery , how can I validate if a div exists in body of
Using jQuery , I can use the following function to execute code as soon
Using jQuery I'm programmatically generating a bunch of div 's like this: <div class=mydivclass

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.