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

103
Vistas
Jquery function value changes unexpectedly with external variable change

Currently, I have a code that keeps updating a list of "cards", during setup it applies this code to add an edit button that fires a universal modal:

$("#C" + cardID + "Edit").off('click').click(function() {
    editCard(cardID);
});

Where cardID is in a loop that sets every card, and goes from 1 to 5 for my cards (C1Edit, C2Edit, etc). I am using .off('click') as currently the same code that keeps the values updated adds the events to the buttons in the cards.

The function editCard() is as follows:

function editCard(id) {
    selectedCard = id;
    $("#cardEditName").val($("#C" + id + "T").html());
    $("#cardEditUnit").val($("#C" + id + "U").html());
}

The issue is, when I click on the button #C2Edit, I get an id of 5. Which means that somehow the function is trying to access the current value of cardID, instead of taking the value when button was created.

I am not really sure how to resolve this issue, or if there is something obvious that I missed (which I expect to be the case).

My loop:

for (element in result) {
   cardID = element;
   .
   .
   $("#C" + cardID + "Edit").off('click').click(function() {
    editCard(cardID);
   });
}

I know that my loop should work fine, as other fields that are set (I am setting a number of fields at once) are appearing correctly as far as I can see.

about 4 years ago · Juan Pablo Isaza
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