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> 
  • 2
    Have you ever tried to do something for this? and where is ajax? - Igor
  • I know how to create randomly, but how can I make Active class jump every 3 seconds randomly from one <div class = "item"> to another, without reloading the page? The ajax script performs functions in real time, therefore, it added. - Harts
  • 2
    This question should be closed, because the question from the category "Write the code for me" - Yuri

1 answer 1

 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> 

  • Thanks for the response, apparently I did not correctly composed the question. I need the code to work every 3 seconds without reloading the page. - Harts
  • check I added - L. Vadim
  • yes every 3 seconds - L. Vadim
  • After 3 seconds, he adds active to all <div class = "item"> tags. But it is necessary that the class active be added randomly to one of the <div class = "item"> tags and every 3 seconds randomly jump from one <div class = "item"> to another <div class = "item">. - Harts
  • Example: sk-ellada.com - Harts