I have a page that looks like
<a href="#" onclick="go('register')">
<img class="img" src="img/192.webp" alt="box set" />
<div class="name" style="line-height: 2">box set</div>
</a>
</div>
<div class="item ">
<a href="#" onclick="go('register')">
<img class="img" src="img/182.webp" alt="battery" />
<div class="name" style="line-height: 2">battery</div>
I was wondering if there is a way that I could insert a div class randomly "percentx" (random between 1-100), so when the page is loaded, it would look like:
<a href="#" onclick="go('register')">
<img class="img" src="img/192.webp" alt="box set" />
<div class="name" style="line-height: 2">box set</div>
<div class="percent75"></div>
</a>
</div>
<div class="item ">
<a href="#" onclick="go('register')">
<img class="img" src="img/182.webp" alt="battery" />
<div class="name" style="line-height: 2">battery</div>
<div class="percent61"></div>
Then change every 60 second
<a href="#" onclick="go('register')">
<img class="img" src="img/192.webp" alt="box set" />
<div class="name" style="line-height: 2">box set</div>
<div class="percent21"></div>
</a>
</div>
<div class="item ">
<a href="#" onclick="go('register')">
<img class="img" src="img/182.webp" alt="battery" />
<div class="name" style="line-height: 2">battery</div>
<div class="percent95"></div>
Regarding this thread
$('.percent').each(function () {
var x = Math.floor((Math.random() * 100) + 1);
$(this).text(x)
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="percent"></div>
Honestly I have no idea how to modified this code for my needs, any helps would highly apreciated.
2
Answers
@ebod here is the modified code,
and
using the var x value
if (x < 50)
<a class="items "
did not change anyif (x > 50 )
<a class="items "
change to<a class="items hot "
next add another class<img class="overlay-img" src="css/img/logo-hot.gif" >
for cosmetic purposes.however, this code only change the first children of group_flex class, and for some reason the value < 50 sometimes change the class items to items hot.
Please help.
Taking a look at the snippet you provided, you can use
$('div[class*="percent"]')
which will select class names containing the substring ‘percent’. I would suggest using ID names if you want the random percentages to be unique per div. Otherwise, the solution below will overwrite all class names every 60 seconds that contain ‘percent’ with ‘percentx’ where x is the randomly generated number. Hope this helps!