0

I have 4 div items on my page, I'm trying to get the inner html of these items to fade out randomly, take a row from an array and fade that in - in its place, does this make sense?

// My items that I would like to repopulate the faded out info

var testimonialsArray = [
    "<div class='change'><h1>Box 5</h1><p>Lorem ipsum</p></div>", 
    "<div class='change'><h1>Box 6</h1><p>Lorem ipsum</p></div>", 
    "<div class='change'><h1>Box 7</h1><p>Lorem ipsum</p></div>", 
    "<div class='change'><h1>Box 8</h1><p>Lorem ipsum</p></div>", 
    "<div class='change'><h1>Box 9</h1><p>Lorem ipsum</p></div>" 
];

// Find a random div and fade it out

var order = Math.floor((Math.random()* $('.change').length )+1);

$('.box').eq(order).find('.change').fadeOut();

.......Fade in new data from an array row

http://jsfiddle.net/crn7dysu/1/

3
  • 1
    This code never generates 0 as index Commented May 12, 2015 at 10:15
  • So, aside from the 0 index problem, what is your actual problem? You do not actually say what is not working the way you want. Commented May 12, 2015 at 10:20
  • You mean something like: $('.box').fadeOut(500).html( getRandomDiv() ).fadeIn(500); ? Commented May 12, 2015 at 10:26

3 Answers 3

2

Based on what I think you wanted to do:

var order = Math.floor((Math.random()* $('.change').length ));

$('.box').eq(order).find('.change').fadeOut(function(){
   $(this).html(testimonialsArray[Math.floor((Math.random()* testimonialsArray.length))]).fadeIn();
});

JSFiddle: http://jsfiddle.net/crn7dysu/4/

Sign up to request clarification or add additional context in comments.

Comments

0

You could use this script:

var testimonialsArray = [
    "<div class='change'><h1>Box 5</h1><p>Lorem ipsum</p></div>", 
    "<div class='change'><h1>Box 6</h1><p>Lorem ipsum</p></div>", 
    "<div class='change'><h1>Box 7</h1><p>Lorem ipsum</p></div>", 
    "<div class='change'><h1>Box 8</h1><p>Lorem ipsum</p></div>", 
    "<div class='change'><h1>Box 9</h1><p>Lorem ipsum</p></div>" 
];

var order = Math.floor((Math.random()* $('.box').length )+1);
var newOrder = Math.floor((Math.random()* testimonialsArray.length ));

$('.box:nth-child('+order+')').find(".change").fadeOut( function() {
    $(testimonialsArray[newOrder]).appendTo('.box:nth-child('+order+')').hide().fadeIn('slow');
});

Updated Fiddle

1 Comment

Constructing a string selector, just to use :nth-child, seems a little wasteful don't you think? The original code was shorter/faster (but just had an incorrect +1 index).
0

Suppose you get 0 from Math.random() and that $('.change').length is 10

Math.floor((0 * 10) + 1);

You can see that this code will never generate 0 as output.

This one instead will generate all the possible combinations:

Math.floor(Math.random()* $('.change').length);

1 Comment

The +1 was certainly wrong, but length-1 is also incorrect as Math.random returns "up to but not including 1 (exclusive)" so never hits the length value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.