Question: How to create a script that randomly adds the Active class to any tag in real time? with item class.
<div> <div class="item"></div> <div class="item"></div> <div class="item"></div> </div> Question: How to create a script that randomly adds the Active class to any tag in real time? with item class.
<div> <div class="item"></div> <div class="item"></div> <div class="item"></div> </div> setInterval(function(){ var random = parseInt((Math.random()) * 3); var remove = parseInt((Math.random()) * 3); var res = document.querySelectorAll('.item'); res[random].classList.add('active'); if(random != remove) res[remove].classList.remove('active'); },3000); .active { background: red; width:200px; height:100px; border:1px solid black; } .item { width:200px; height:100px; border:1px solid black; } <div> <div class="item"></div> <div class="item"></div> <div class="item"></div> </div> Source: https://ru.stackoverflow.com/questions/619431/
All Articles