Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to show random elements from a list

Status
Not open for further replies.

speedster81

Programmer
Oct 4, 2010
2
0
0
GB
Hi,

I have a dynamically generated list of which contain div's with a class of product. I want to only display 6 random products out of the whole list on page load and hide the rest. I am quite new to javascript so some help would be much appreciated.

Thanks
 
Set all your divs to be hidden to begin with and then use getElementsByClassName() to get all your "product" divs, and then just randomly select 6 to show.


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
[ ]

I use the following function to generate a random number. Set dv to the maximum number of choices you have then call this function. Repeat it 6 times. If you get a duplicate, discard one, then call this function again.



[blue][tt]function Getrandommod(dv){
var zz = new Date();
zz = Math.floor(zz.getTime()/1000);
zz = zz % dv + 1;
return zz;
}

[/tt][/blue]

mmerlinn


"We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding. Answering questions for careless and sloppy thinkers is not rewarding." - Eric Raymond

Poor people do not hire employees. If you soak the rich, who are you going to work for?
 
Hi

mmerlinn said:
I use the following function to generate a random number.
Why would that be better than [tt]Math.random()[/tt] ?
mmerlinn said:
Repeat it 6 times. If you get a duplicate, discard one, then call this function again.
Yes, but because you are dividing the [tt]getTime()[/tt] with 1000, that means you will get a different value only one second later. So with your function you will have to wait 6 seconds to get 6 distinct, but consecutive numbers.

Feherke.
 
[ ]
feherke

I have no idea whether it is better or not. My specialty is FoxPro, but I have had to learn a smattering of JS to insert into my FP programs. This routine is something I put together years ago when I was starting to learn JS and I have used it ever since (like, why change what works without a very good reason?)

In my case, I usually don't care if there are duplicates even though this function RARELY produces duplicates for me. I just made him aware that this function COULD produce duplicates, which in his case are NOT wanted.

mmerlinn


"We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding. Answering questions for careless and sloppy thinkers is not rewarding." - Eric Raymond

Poor people do not hire employees. If you soak the rich, who are you going to work for?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top