Untitled Snippet

language: Other
license: Other

Code for Snippet:

		A fellow RapidWeaver colleague asked my advice
		on the jQuery code required to build a Stack that would
		allow the user to drop in up to a dozen Stacks
		and only show one random Stack on each page load...
		and I do love a challenge!
		Here was my thinking on it. Each div needs to have two
		id's/classes, one that is common between them and one that
		is unique to each. For our sake here I am calling the
		common one .hideStuff and the unique ones .showStuff+
		(where "+" is 1 - 12).
	var $showStuff = new Array();// lets make an array to represent the users stuff
	var i=0;
	while (i<=11) {// you could just as easily use a for loop here
		if($('.showStuff'+i).length){// we test to see if a .showStuff+ object exists
			$showStuff[i] = $('.showStuff'+i);// we assign it an array value
	// And then you want to generate the randomness with Math
	var showRandom = Math.floor(Math.random()*$showStuff.length);
	// Hide all the divs with the common class
	// Show one of the random ones
		With a little bit of HTML you get a randomly appearing div on
		each page load... like so:
		<div class="hideStuff showStuff1">
			this is the FIRST div	
		<div class="hideStuff showStuff2">
			this is the SECOND div	
		<div class="hideStuff showStuff3">
			this is the THIRD div	
		<div class="hideStuff showStuff4">
			this is the FOURTH div	
comments powered by Disqus


Link to this snippet:

Download to Code Collector

To use the direct link to your snippet on CodeCollector.net either copy the html from the above section or drag the Download to Code Collector to where you would like to use it.

More Info:

Times Viewed: 777
Date Added: 2014-01-10 04:34:28
Last Modified: 2014-01-09 21:34:42

Web Analytics