Forum Moderators: open

Message Too Old, No Replies

random onload of divs - is it possible?

         

SBee

3:24 pm on Mar 25, 2008 (gmt 0)

10+ Year Member



Hi-

I have used javascript to be able to load a random image (from an array) on page load in the past. Is this possible to do with a div of content?
I have 3 areas on a page that the client wants to randomize - i know i could do it if the content was flash, or images, not sure if i can with html text in a div...

Thanks in advance.
Shawna

venti

5:52 am on Mar 26, 2008 (gmt 0)

10+ Year Member



It certainly is possible and many different techniques could be used to accomplish this. A pure client side way to do this off the top of my head.. set the three divs display attribute to hidden via CSS. Then have the JS randomly select one of the divs and set the display attribute to block or inline on page load.

gcarn

1:21 am on Mar 28, 2008 (gmt 0)

10+ Year Member



yep, lots of ways to take at it..
i would slap an id on the div,
and then update the contents of the div with data stored in your array with innerHTML

var randomID = Math.ceil(100*Math.random())
var UpdateDiv = document.getElementById('yourDivID');UpdateDiv.innerHTML=yourArray[randomID];

<div id="yourDivID"></div>

i would think that should work easily