JSFiddle - React, Tailwind, and code Playground
by Alejandro
HTML
<p class="btn">Grab a random block!</p>
<div class="blocks">
<div>Block 1</div>
<div>Block 2</div>
<div>Block 3</div>
<div>Block 4</div>
<div>Block 5</div>
<div>Block 6</div>
<div>Block 7</div>
<div>Block 8</div>
<div>Block 9</div>
<div>Block 10</div>
</div>
CSS
.blocks div {
height: 60px;
width: 110px;
background-color: blue;
color: #fff;
margin: 5px;
display: inline-block;
padding: 40px 10px 10px 10px;
font-weight: bold;
font-size: 20px;
text-align: center;
display: none;
}
.blocks div:nth-child(2n) {
background-color: #5993e0;
}
.btn {
background-color: gray;
padding: 7px 15px;
width: 160px;
color: #fff;
border-radius: 5px;
text-align: center;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
.btn:hover {
cursor: pointer;
}
JavaScript
$('.btn').click( function() {
var count = $('.blocks div').length;
var randomer = Math.ceil(Math.random() * count)
$('.blocks div').each(function () {
var pos = $(this).index() + 1;
$(this).hide();
if (pos == randomer) {
$(this).show();
}
});
});