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

175
Vistas
eliminar el elemento li cuando la casilla de verificación no está marcada

Puedo agregar dinámicamente un nuevo elemento li en el evento marcado de una casilla de verificación. Sin embargo, no puedo eliminar lo mismo en el evento unchecked .

 $(document).ready(function() { var value; $(".tmCheckbox").change(function() { if (this.checked) { value = $(this).val(); $('.ul_sec').append($('<li>', { text: value })); } else { // $(this).parent().remove(); } }); });

CÓDIGO HTML:

 <h3>MY SELECTION </h3> <ul class="ul_sec" style="display: block;"> <li><input type="checkbox" name="filter" value="">Random Text 1</li> <li><input type="checkbox" name="filter" value="">Random Text 2</li> </ul> <h3>FILTER</h3> <ul style="display: block;"> <li><input class="tmCheckbox" type="checkbox" name="filter" value="Random Text 1">Random Text 1</li> <li><input class="tmCheckbox" type="checkbox" name="filter" value="Random Text 2">Random Text 2</li> <li><input class="tmCheckbox" type="checkbox" name="filter" value="Random Text 3">Random Text 3</li> </ul>
about 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

Filtre el elemento li según el contenido del texto usando el método filter() y elimínelo usando el método remove() .

 var $this = this; $('.ul_sec li').filter(function(){ return $(this).text() == $this.value; }).remove(); 

 $(document).ready(function() { var value; $(".tmCheckbox").change(function() { var value = $(this).val(); if (this.checked) { $('.ul_sec').append($('<li>', { html: this.outerHTML + value })); } else { $('.ul_sec li').filter(function() { return $(this).text() == value; }).remove(); } }); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <h3>MY SELECTION </h3> <ul class="ul_sec" style="display: block;"> </ul> <h3>FILTER</h3> <ul style="display: block;"> <li><input class="tmCheckbox" type="checkbox" name="filter" value="Random Text 1">Random Text 1</li> <li><input class="tmCheckbox" type="checkbox" name="filter" value="Random Text 2">Random Text 2</li> <li><input class="tmCheckbox" type="checkbox" name="filter" value="Random Text 3">Random Text 3</li> </ul>

about 4 years ago · Santiago Trujillo Denunciar

0

Simplemente use remove() en el destino donde se adjunta

Como esto:

 $(document).ready(function() { var value; $(".tmCheckbox").change(function() { if (this.checked) { value = $(this).val(); $('.ul_sec').append($('<li>', { text: value })); } else { $('.ul_sec li').last().remove(); } }); });
about 4 years ago · Santiago Trujillo Denunciar

0

document.getElementById("yourid").remove(); dando una identificación al <li> que acaba de agregar.

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