Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

129
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda