javascript - pick certain amount of child elements from the dom -


i have example 15 div tags class name in page

<div class="classname">content</div> <div class="classname">content</div> <div class="classname">content</div> <div class="classname">content</div> <div class="classname">content</div> <div class="classname">content</div> <div class="classname">content</div> <div class="classname">content</div> <div class="classname">content</div> <div class="classname">content</div> <div class="classname">content</div> <div class="classname">content</div> <div class="classname">content</div> <div class="classname">content</div> <div class="classname">content</div> 

and can select them using jquery

var targetdivs = $(' .classname '); 

it going return 15 div tags in case want randomly pick 9 of them , store them in variable

you use jquery each , build unique , random element array.
loop out on element array place elements want randomization occur.

var divs = []; var indexs = [];   while(indexs.length < 9){    var num = math.floor(math.random() * 9) + 1;   indexs.push(num);   indexs = $.unique(indexs); }  $('.classname').each(function(index, element){      if(indexs[index]){         divs.push($('.classname').eq(indexs[index]));     } }); console.log(divs); 

Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -