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
Delete a list item element with Jquery

I made a simple form that takes an input name and when I click submit, it will append the output name into an unordered list with a delete button beside each item. But when I click the delete (X) button, it still leave the X button there, and when I have multiple list, it remove all the list instead of the list that corresponds with each item.

view view1

$("button#submit").click((e) => {
  e.preventDefault();
  let name = $("#name").val();
  console.log(name);
  $("ul#submittedName").append(
    "<div class='container'><div class='row'><div class='col-sm'><li id='nameList'>" +
    name +
    "</div><div class='col-sm'><a class='delete' href='#'>X</a></div></div></div></li>"
  );
  $("#name").val("");
});

$("ul").on("click", ".delete", function(e) {
  console.log(e.target);
  $("li#nameList").remove();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous" />
<form id="formName" class="align-center p-3 mx-auto">
  <div class="mb-3">
    <label for="exampleInputEmail1" class="form-label">Name</label>
    <input type="text" class="form-control" id="name" aria-describedby="emailHelp" />
  </div>
  <button id="submit" type="submit" value="Submit" class="btn btn-primary">
        Submit
      </button>
</form>
<ul id="submittedName" class="align-center p-3 mx-auto"></ul>

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

0

As stated in the comments, id's are meant to be unique, but you can do this without the use of the id at all, and since you want to remove the delete button as well, just get the ancestor div.container and remove it, this will remove the li and the delete button:

$("ul").on("click", ".delete", function(e) {
   $(this).closest('div.container').remove();
});

If you fix your HTML to be complient, all you need to do is, $(this).closest('li').remove() in your event handler. Here is an HTML complient example for what you are trying to do

    $(function(){
        $("button#submit").click((e) => {
            e.preventDefault();
            let name = $("#name").val();
      
            $("ul#submittedName").append("<li>" + name + "&nbsp;<a class='delete' href='#'>X</a></li>");
            $("#name").val("");
        });

        $("ul").on("click", ".delete", function(e) {
            $(this).closest('li').remove();
        });
  });
   
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous" />
    <form id="formName" class="align-center p-3 mx-auto">
      <div class="mb-3">
        <label for="exampleInputEmail1" class="form-label">Name</label>
        <input type="text" class="form-control" id="name" aria-describedby="emailHelp" />
      </div>
      <button id="submit" type="submit" value="Submit" class="btn btn-primary">
            Submit
       </button>
    </form>
    <ul id="submittedName" class="align-center p-3 mx-auto"></ul>


    

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