Here is exactly what you need.
$(document).ready(function(){
var element = $('.row__seat'),
maxLimit = element.length - 1,
minLimit = 0,
requiredRandomSelection = 3
$(element).each(function(){
$(this).removeClass('tooltip');
$(this).removeClass('row__seat--reserved');
});
var selectedIndex = [];
while(selectedIndex.length !== requiredRandomSelection){
var rand = Math.floor(Math.random() * (maxLimit - minLimit + 1)) + minLimit;
if( selectedIndex.indexOf(rand) === -1){
selectedIndex.push(rand);
$(element[rand]).addClass('row__seat--reserved');
}
}
$(element).each(function(index){
if(selectedIndex.indexOf(index) === -1){
$(this).addClass('tooltip');
}
});
});
.tooltip{
color: green;
}
.row__seat--reserved{
color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row">
<div class="row__seat tooltip" data-tooltip="A1">aaaa</div>
<div class="row__seat tooltip" data-tooltip="A2">bbbb</div>
<div class="row__seat tooltip" data-tooltip="A3">cccc</div>
<div class="row__seat tooltip" data-tooltip="A4">dddd</div>
<div class="row__seat tooltip" data-tooltip="A5">eeee</div>
<div class="row__seat tooltip" data-tooltip="A6">ffff</div>
<div class="row__seat tooltip" data-tooltip="A7">gggg</div>
<div class="row__seat tooltip" data-tooltip="A8">hhhh</div>
<div class="row__seat tooltip" data-tooltip="A9">iiii</div>
<div class="row__seat row__seat--reserved">jjjj</div>
<div class="row__seat row__seat--reserved">kkkk</div>
<div class="row__seat row__seat--reserved">llll</div>
<div class="row__seat tooltip" data-tooltip="A13">mmmm</div>
</div>
For understanding i have added the css property for tooltip
and row__seat--reserved
so that you can observe the change. To test just click on 'Run Code Snippet' button again and again. Also, i have set the variable requiredRandomSelection
as 3
which means that you need to have only 3 elements with the class row__seat--reserved
which is randomly selected. You can simply increase this value but remember it cannot be greater than the number of elements with class row__seat
. The code also make sure that the random elements are not repeated, hence it picks the unique <div>
.