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

157
Vistas
Why is the id value changing after onClick event?

I am developing a simple website in HTML and trying to implement a delete function in JavaScript that takes an id value passed within the onClick parameters. It is then concatenated with API URIs to delete from a database. When the onClick event executes the id value is changed and I cannot understand why.

The getImages() function retrieves data from the API and appends it to a list, adding the delete button for each image.

An example id when fetched for the getImages() function shows in the console log: 637767724469985182

function getImages(){

  $('#ImageList').html('<div class="spinner-border" role="status"><span class="sr-only"> 
  &nbsp;</span>');
  $.getJSON(IMAGERAA, function( data ) {

  var items = [];

  $.each( data, function( key, val ) {
   console.log(val["id"]);
  items.push('<button type="button" id="deleteAsset" class="btn btndanger" 
  onclick="deleteAsset('+val["id"] +')">Delete</button> <br/><br/>');
  items.push( "<hr />");   
  });

  $('#ImageList').empty();
  $( "<ul/>", {
  "class": "my-new-list",
  html: items.join( "" )
  }).appendTo( "#ImageList" );
  });
}

The console log then shows that the id when passed to the delete function has changed to 637767724469985200.

function deleteAsset(id){
 console.log(id);
 $.ajax({
 type: "DELETE",
 url: IMAGEDIA1 + id + IMAGEDIA2,
 }).done(function( msg ) {
 getImages();
 });
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You are missing a set of quotes around the string passed into deleteAsset() in the html

Try changing:

onclick="deleteAsset('+val["id"] +')"

to

onclick="deleteAsset(\"'+val["id"] +'\")"
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