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

232
Vistas
How to remove a part of the content of a div with jQuery?

When I click on a checkbox, I append some content to a div (#item_list)

    if(cb_new.prop('checked')) {
        $("#item_list").append("<input type='hidden' name='post[]' value="+ this.value +">");
        
    } else {            
        // ??           
    }

When I uncheck the box I want that exact same string to be removed from the div. Keeping all the others that have a different value (this.value).

So for example I could have:

<div id="item_list"> 
<input type="hidden" name="post[]" value="102">
<input type="hidden" name="post[]" value="93">
</div>

But if I uncheck

<input type="checkbox" id="d-102-2" name="d-102" value="102" data-id="d-102"> 

I want :

<div id="item_list"> 
<input type="hidden" name="post[]" value="93"> 
</div>

If I check it again, I want it back.

How can I do that?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

A vanilla JS solution would look like:

Updated according to your expanded requirements.

document.querySelector(`#item_list input[value='${this.value}']`).remove();

This will query the DOM, find an input element, with a value attribute whose value is equal to this.value, and remove it from the DOM with the remove() method.

A more detailed implementation isn't easy to give without having more information.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use the data attribute to assign unique id to the checkbox, once it is checked, input element with same data-uid is added and once unchecked we remove the input element with same data-uid

  $(document).ready(function() {
    $("#cb_new").change(function() {
      if ($(this).prop('checked')) {
        $("#item_list").append($("<input data-uid='"+$(this).data('uid')+"' type='text' name='post[]' class='newItem' value='" + $(this).val() + "'>"));
      } else {
        $('.newItem[data-uid='+$(this).data('uid')+']').remove();
      }
    });
  });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input type="checkbox" id="cb_new" data-uid="8080" value="tick Me" name="test"/><label for="test">Tick Me</label>
<div id="item_list" style="border:1px solid tomato">

</div>

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