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

127
Views
adding style to li elements if checkbox is checked

so below is my code for the Ul and Li elements.

 <ul id = "to-do-list" >
            <li> <input type="checkbox"> to school</li>
            <li> <input type="checkbox"> do website</li>
        </ul>

it is for a To do list: code for js function to add task items is below as well.

document.getElementById("submit-task").onclick = function (){
    
    let li = document.createElement("li");
    let text = document.getElementById("task").value; 
    let checkbox = document.createElement("input");
    checkbox.type ="checkbox";
    checkbox.value = text;
    checkbox.checked = false;

    li.appendChild(checkbox);

    let textnode = document.createTextNode(text);

    li.appendChild(textnode);

    if (text === ''){
        alert("There is no task entered");
    }
    else{
        document.getElementById("to-do-list").appendChild(li);
    } 
    document.getElementById("task").value="";
    
};

I want to change the li text if a checkbox is checked. how can I do this?

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

0

Though DCR's solution is good, it doesn't work if the input gets unchecked. Here's a little modification that i've made, hope it will solve the problem!

function changeOnChecked(){
 const a = event.target.closest('input').checked;
 if(a)
  event.target.closest('li').style.color='red'
 else
   event.target.closest('li').style.color='black'
}
<ul id = "to-do-list" >
            <li> <input type="checkbox" onclick="changeOnChecked()"> to school</li>
            <li> <input type="checkbox" onclick="changeOnChecked()"> do website</li>
        </ul>

about 4 years ago · Juan Pablo Isaza Report

0

You can add a 'change' event listener on the checkboxes and then apply a particular style or class to the parent element.

Add following JS code to your project:

    const checkBoxes = document.querySelectorAll("#to-do-list input")

    checkBoxes.forEach(checkBox => {
      checkBox.addEventListener("change", function (e) {
        const parentNode = e.target.parentNode

        if (e.target.checked)
          parentNode.style.color = "red"
        else
          parentNode.style.color = "black"
      })
    })
about 4 years ago · Juan Pablo Isaza Report

0

The way you have got it set up isn't recommended, and what I mean by that is, you have the input checkbox inside the li tag along with the text so you won't be able to change it.

This is what I would do:

<li><input type="checkbox" data-num=0/><span id="t0">text goes here</span></li>

You can notice in the checkbox I have added a data-num attribute, this will contain the global variable number which increments after adding a task. It will help to identify the span.

To set the event listener you can do this after creating the event:

checkbox.onclick = changeText(event)


function changeText(e){
    let elem = e.target;

    let span = document.querySelector("#t"+elem.getAttribute('data-num');
    
    if(elem.checked)
        span.innerHTML = "It is checked";
    else
        span.innerHTML = "It is not checked"'
}
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!