Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

151
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!