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

209
Vistas
How can I delete an element in an object?

I have a checkbox which add an id of his value in array when checked and I want to delete this value when I uncheck it

I tried to remove my id with and indexOf() + splice() but I can't use indexOf() because I'm using an object

Some one have an idea to how can I delete my id when I uncheck my checkbox, or if there is a trick to use indexOf with an object?

there is my script :

$(document).ready(function() {

  const formInputIds = $('form#export input[name="ids"]');

  $('.exportCheckbox:checkbox').on('change', function() {
    const announceId = $(this).data('id');
    if (this.checked) {
      formInputIds.push(announceId);
      console.log(formInputIds);
    } else {
      const index = formInputIds.val().indexOf(announceId);
      if (index > -1) {
        formInputIds.val().splice(index, 1);
      }
      console.log(formInputIds);
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="export" action="exportAnnounces">
  <input type="hidden" name="ids" value="[]" />
  <button type="submit" class="btn btn-primary">Download</button>
</form>


<some data of product displayed>

  <input type="checkbox" data-id="{{annonce._id}}" class="exportCheckbox"/>

there is the console.log of formInputIds with 3 ids :

the console.log of formInputIds

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

0

Consider the following.

$(function() {

  var formInputIds;

  function getChecked(target) {
    var results = [];
    $("input[type='checkbox']", target).each(function(i, elem) {
      if ($(elem).is(":checked")) {
        results.push($(elem).data("id"));
      }
    });
    return results;
  }

  $('.exportCheckbox').on('change', function(event) {
    formInputIds = getChecked($(this).parent());
    console.log(formInputIds);
  });

  $("#export").submit(function(event) {
    event.preventDefault();
    console.log(formInputIds);
    $("[name='ids']", this).val("[" + formInputIds.toString() + "]");
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="export" action="exportAnnounces">
  <input type="hidden" name="ids" value="[]" />
  <button type="submit" class="btn btn-primary">Download</button>
</form>


<div class="some content">
  <input type="checkbox" data-id="1001" class="exportCheckbox" />
  <input type="checkbox" data-id="1002" class="exportCheckbox" />
  <input type="checkbox" data-id="1003" class="exportCheckbox" />
  <input type="checkbox" data-id="1004" class="exportCheckbox" />
  <input type="checkbox" data-id="1005" class="exportCheckbox" />
  <input type="checkbox" data-id="1006" class="exportCheckbox" />
</div>

This way, you build the Array based on just the checked items. No need to find and slice the exact item.

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