Solution 1 :

But after I click delete on my first element, the innerHTML value
remains the same.

That’s because you’re not setting the innterHTML to empty text. You could set it to empty text based on the successful response of axio.

 let movieToDelete = document.querySelector('.id');
 let movieToDeleteID = movieToDelete.movieToDeleteID
await'http://localhost:5000/movies/delete', {movieToDeleteID})
    .then( response => {
       movieToDelete.innerHTML = "";
       //Or, movieToDelete.parentNode.removeChild(movieToDelete);
    .catch(err =>  console.error(err));

Problem :

So I retrieve a list of data and each piece of data has an ID provided by SQL. The ID is stored in a

tag, and I extract the ID from the

tag using innerHTML.

I have an event handler that will delete the element on click based on its ID, But after I click delete on my first element, the innerHTML value remains the same. For example, say the ID of the first element was 10, once I click delete, it will delete it. But once I click delete on the element with the ID of 11, it still says the value is 10 and will NOT delete the element with the ID of 11.

I’ve tried changing the P element between ID and class. I have tried to use .textContent and getElementById() With my click handler. But they all have given the same result.

This is the code for generating the HTML from GET request.

getMoviesBtn.addEventListener('click', async(e) => {
    await axios.get('http://localhost:5000/movies/retrieve-movies')
      .then( res => {
        const movieList =
        for (let i = 0; i<movieList.length; i++) {
        const sections = document.createElement('section')
        sections.innerHTML = `
        <p class='id'> ${movieList[i].id} </p>
        <p> Title: ${movieList[i].title} </p>
        <p> Runtime: ${movieList[i].runtime} </p>
        <p> Release date: ${movieList[i].releaseDate} </p>
        <button class="delete-btn">Delete</button>

This is the code for deleting the movie entry.

  // Delete movie
document.addEventListener('click', async(e) => {

  if( && === 'Delete') {

  // Reset the notification bar to be displayed again

  let movieToDeleteID = document.querySelector('.id').innerHTML;

  await'http://localhost:5000/movies/delete', {movieToDeleteID})
    .then( response => {
    .catch(err =>  console.error(err));

The HTML i’m using is

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" type="text/css" href="./css/main.css"></link>
    <link href=";600&display=swap" rel="stylesheet">

  <div id="notification"> 
    <a id="close"> Close </a>

    <a href="./index.html">Create Movie</a>

  <div id="get-movies-btn-div">
    <button id='get-movies-btn'>Get all movies</button>

  <div id='movie-list'></div>

  <script src="../node_modules/axios/dist/axios.min.js"></script>
  <script src="movieList.js"></script>

Thank you for the help!


Comment posted by Jason Pallone

I figured it out thanks! For anyone else having a similar issue, I had to delete the element (in my specific case I deleted the parent element because that was my end goal was to delete it in the database and remove the elements from the page) so I did that removed the whole section element I created and solved my issue!