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

149
Views
Style Property inside a Class in JavaScript is not getting updated
let taskList = document.getElementById("taskList");
const newItem = new Item("some value");
taskList.append(newItem.item)

const updateTaskList = () => {
    taskList.innerHTML = "";
    taskItems = JSON.parse(localStorage.getItem("tasks"));
    const div = document.createElement("div");
    div.innerHTML = taskItems;
    taskItems = div.querySelectorAll("li");
    taskItems.forEach((task) => {
    taskList.append(new Item("some value").item);
    })    
}

const updateLocal = () => {
    taskList = document.getElementById("taskList");
    localStorage.setItem("tasks", JSON.stringify(taskList.innerHTML));
}

class Item {
    constructor(name){
        const div = document.createElement("div");
        div.innerHTML = name;
        this.item = div;
        this.item.addEventListener("click", () => this.changeStyle());
    }

    changeStyle(){
        this.item.style.textDecoration = "line-through";
        updateLocal();
    }
}
document.addEventListener("DOMContentLoaded", () => {
    updateTaskList();
})

The style gets updated in the DOM, but after reloading the page the style disappears instead of getting stored locally. What's the problem here.

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

0

Turns out, I was creating a new element from the Class, and not updating the style. All I had to do was pass in the styles from the local storage to the class, and update the styles for the new element.

class Item {
constructor(name, styles){
    const div = document.createElement("div");
    div.innerHTML = name;
    if(styles) div.styles.textDecoration = styles.textDecoration;
    this.item = div;
    this.item.addEventListener("click", () => this.changeStyle());
}

changeStyle(){
    this.item.style.textDecoration = "line-through";
    updateLocal();
}

}

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!