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

219
Views
Uncaught TypeError: Cannot read property of undefined (reading 'green')
const mainebutton = document.querySelector("button");
const body = document.body;

function randomcolorgenerator() {
  const red = Math.floor(Math.random() * 256);
  const green = Math.floor(Math.random() * 256);
  const blue = Math.floor(Math.random() * 256);
  console.log(red, green, blue);
  const randomcolor = "rgb ${red},${green},${blue}";
  return randomcolor;
}
mainebutton.addEventListener("click", () => {
  const randomcolor = randomcolorgenerator();
  console.log(randomcolor);
});

Uncaught TypeError: Cannot read property of undefined (reading 'green')

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

0

You need to add a 'DOMContentLoaded' event listener to wait for your content to load before accessing them

And adding what @Luca has mentioned, this should be a template string:

"rgb ${red},${green},${blue}" should be enclosed in `` not ""

document.addEventListener('DOMContentLoaded', function(){
    const mainebutton = document.querySelector("button");
    const body = document.body;
    function randomcolorgenerator() {
        const red = Math.floor(Math.random() * 256);
        const green = Math.floor(Math.random() * 256);
        const blue = Math.floor(Math.random() * 256);console.log(red, green, blue);
        const randomcolor = `rgb ${red},${green},${blue}`;
        return randomcolor;
    }
    mainebutton.addEventListener("click", () => {
        const randomcolor = randomcolorgenerator();
        console.log(randomcolor);
    });
})
<button>SHOW ME A COLOR</button>

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!