Jquery Randomizing within Categories
so here is my question in a situation if you will. i have 6 items in div c开发者_如何学JAVAlasses which correspond to 3 categories something like this
class ="boat"
class ="car"
class ="bike"
class ="blueboat"
class ="redcar"
class ="greenbike"
so is there a jquery/javascript approach with .addClass
where when someone clicks on the button "Randomize" it adds class .mine
to each one of the 3 categories ie blueboat.mine
and redcar.mine
and bike.mine
but not boat
or car
or greenbike
because they are in the same category?
Or another situation rather than .addClass
use a jquery/javascript so when "Randomize" is clicked to randomly show 3 out of 6 items one in each category of boat
, car
, bike
and hide the rest blueboat
, redcar
, greenbike
. in this situation all 6 items are on the page when document is ready but with the randomize only 3 are left one from each category.
please let me know if anyone has any ideas or needs further clarification any help is greatly appreciated.
thank you -david
edit: maybe this image will help out https://dl-web.dropbox.com/get/rand.jpg?w=fc89ef22 I have added three categories A/B/C in those categories i have three items, A)car,boat,bike B)dog,cat,mouse C)Apple, Orange,Kiwi.. so when user clicks Randomize it randomly selects one item from each category (A,B,C) and adds the class .mine to each one of them. so i random and get car,dog,orange (.mine) for each. then i click randomize again and get something else ie: boat,mouse,kiwi there is a possible only 27 combinations for this idea... i hope this clarifies my intents..hopefully it did not complicate matters more.
may be this helps DEMO
update
you can use toggle
see the EXAMPLE
take a look here Demo
Here i change the background color of divs which have class car
, bike
and boat
Reference
Please clarify it a bit, what are the three categoires, if classes are of 6 items
let there categories means 3 classes, "bike" , "boat" , "car", for specialization you should add specific class to this like "red bike", instead of "redBike"
According to my assumption this is the answer,
function (){
var category = ["car" , "boat" , "bike"];
for( var i = 0 ; i < 3 ; i++){
var rand = Math.floor( Math.random() * 2);
$( "div#id ." + category[i]).eq(rand).addClass("highlight");
}
}
精彩评论