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

105
Views
JavaScript Template Class

I'm doing my best to learn JS but I feel like I get stuck!

how can I toggle this kind of template class? I want to set the value {done} of "data-done" property to "true". Here is my code:

document.addEventListener('click', function(e) {
  if (e.target.classList.contains('y row')) {
    e.classList.toggle(data-done = 'true');
  }
})
[data-done='true'] {
  color: red;
}
<span class="y row" data-id="{id}" data-done="{done}" data-priority="{priority}">
     {name} {priority}
</span>

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Syntax error e.classList.toggle(data-done = 'true');

data-done is not a class and you cannot have dashes without quoting and you do not use equal sign when you toggle or set attributes

Also you cannot use contains on two classes. Use .matches(".y.row")

This will toggle

document.addEventListener('click', function(e) {
  const tgt = e.target;
  if (tgt.matches('.y.row')) {
    let done = tgt.dataset.done === "true";
    tgt.dataset.done = !done; 
  }
})
[data-done='true'] {
  color: red;
}
<span class="y row" data-id="{id}" data-done="true" data-priority="{priority}">
     {name} {priority}
</span>

about 4 years ago · Santiago Trujillo 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!