Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Find a random set of elements and appendChild

Find a random set of elements and appendChild

Problem

I'm trying to create a simple board game with pure JavaScript, which I'm trying to make work like 'Whack a Mole'. I know I can do this with the Canvas but feel I'll learn a lot more by not depending too much on it.

I placed all the Elements necessary, which shows the full structure but can't seem to figure out how I can make each object to click on randomly generate.

This is my fiddle, http://jsfiddle.net/JoshuaWaheed/XaUyE/1/

I've written this, var newLI = Math.random(li.appendChild(span));. What I'm trying to tell the program is, "Choose a random set of li's and append a tag into it, which will be clickable," but for some reason it's appending it into all the li's, which I know can be written as li.appendChild(span); instead.

What can I do to fix this? Also is there an event listener of some sort that can see if all the buttons have been clicked and does another random select and append?

Problem courtesy of: Joshua Waheed

Solution

Math.random() returns a number between 0 to 1. You could use the following condition to have some code run or not run randomly:

if( Math.random() 

see how it applies to your code: http://jsfiddle.net/SpacePineapple/XaUyE/3/

Solution courtesy of: TastySpaceApple

Discussion

View additional discussion.



This post first appeared on CSS3 Recipes - The Solution To All Your Style Problems, please read the originial post: here

Share the post

Find a random set of elements and appendChild

×

Subscribe to Css3 Recipes - The Solution To All Your Style Problems

Get updates delivered right to your inbox!

Thank you for your subscription

×