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

291
Views
Why an ID passed via Class' constructor is dynamic?

In my code, I want to pass a unique ID to the li by using a class. However, whenever I refresh the page, the "data-id" attribute is always changing. So the ID becomes different every time the page is reloaded.

Is there a way to make it static but not setting a preset ID?

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div>
    <ul>
        <li id="item">ID check</li>
    </ul>
</div>
<script>
    class IdChecker {
        constructor() {
            this.id = Math.round(Math.random() * 10000);
        }
    }
    let item = document.getElementById('item');
    item.setAttribute('data-id', String(new IdChecker().id))
</script>
</body>
</html>

about 4 years ago · Juan Pablo Isaza
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!