Raffy T Lawrence Raffy T Lawrence - 1 year ago 71
PHP Question

PHP/jQuery: Randomly display single div from loop

I have a loop that contains dynamic and multiple divs. I only need to display single div randomly and when refreshing the page it should change the div dynamically

Like for example PHP

for($i= 1; $i<=5; $i++){
echo'<div class="">
this is div '.$i.'

Output will be


But i want to only display single div. Other should hide using jquery or php, so whenever i refresh the page it should display any number in the loop randomly

//this is div 2

Answer Source

First need to calculate the length of div. Then we hide all other elements and show just the randomly chosen one.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="random">1</div>
<div class="random">2</div>
<div class="random">3</div>
<div class="random">4</div>
<div class="random">5</div>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download