Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

121
Views
How to make the div box randomly change color every 1000 millisecond with HTML Javascript?

I've been trying to figure out how to make the div box randomly change color every 1000 millisecond. This is my attempt of function changeColor JavaScript file:

myInterval = setInterval(changeColor, 1000);
var box = document.getElementById("box");

function changeColor() {
  var randomColor = Math.floor(Math.random() * 16777215).toString(16);

  box.style.backgroundColor = "#" + randomColor;

  //let x = document.getElementById('contents');
  //x.style.backgroundColor = x.style.backgroundColor == "yellow" ? "pink" : "yellow";
}
<div id="box">This is some text in a div element.</div>

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Like this way

var div = document.querySelector(".test");

setInterval(() => {
  div.style.background = `#${getColor()}`;
}, 1000)

function getColor() {    
  return Math.floor(Math.random()*16777215).toString(16);
}
about 4 years ago · Juan Pablo Isaza Report

0

I have attached the javascript function to change the colour of the randomly for every 1000 milliseconds.

<!DOCTYPE html>
<html>
<head>
  <title>Change bg color every 1 seconds</title>
</head>
<body>
<div id="box">This is some text in a div element.</div>
<script>
setInterval(
function () {
  var randomColor = Math.floor(Math.random()*16777215).toString(16);
  document.getElementById("box").style.backgroundColor = "#"+randomColor;
},1000);
</script>
</body>
</html>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!