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

177
Views
¿Hay alguna manera de eliminar solo el elemento en el que se hizo clic cuando todos esos elementos tienen la misma ID?

Quiero implementar la opción "eliminar comentario" cuando hago clic en el comentario. Sin embargo, lo que implementé aquí elimina el comentario en el orden independientemente de en qué haga clic y quiero que elimine uno en el que estoy haciendo clic. Intenté usar for loop para que cada uno obtenga una identificación diferente con un número al lado, pero se volvió demasiado complicado rastrear cuando alguien elimina y agrega nuevamente. Agradecería mucho cualquier idea!

 const createComment = () => { // userId let userId = "userId" // getting comment from input const comment = textbox.value; // element = comment + like button const element = document.createElement('p'); element.id = "individualComment"; element.setAttribute("style", "width:430px"); // like const like = document.createElement("input"); like.id = "like"; like.src = "img/navHeart.jpeg"; like.type = "image" like.setAttribute("style", "height: 15px; width: 15px"); like.style.float = 'right'; // creating element and append it element.innerHTML = userId + " " + comment; whereToAdd.appendChild(element); element.append(like); }
 document.body.addEventListener('click', function (e) { if (e.target.id == 'individualComment') { const element = document.getElementById('individualComment'); element.remove(); } })
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

target.parentNode , vea el fragmento a continuación.

 document.body.addEventListener('click', function (e) { if (e.target.id == 'individualComment') { var target = e.target; var parent = target.parentNode; var index = [].indexOf.call(parent.children, target); parent.children[index].remove(); } })
 <div> <p id="individualComment">Comment 1</p> <p id="individualComment">Comment 2</p> <p id="individualComment">Comment 3</p> <p id="individualComment">Comment 4</p> <p id="individualComment">Comment 5</p> <p id="individualComment">Comment 6</p> </div>

about 4 years ago · Juan Pablo Isaza Report

0

id siempre deben ser únicos en una página. A continuación se muestra una solución basada en el filtrado de una class común:

 document.body.addEventListener('click', function (e) { if (e.target.classList.contains( 'individualComment')) { e.target.remove(); } })
 <div> <p class="individualComment">Comment 1</p> <p class="individualComment">Comment 2</p> <p class="individualComment">Comment 3</p> <p class="individualComment">Comment 4</p> <p class="individualComment">Comment 5</p> <p class="individualComment">Comment 6</p> </div>

about 4 years ago · Juan Pablo Isaza Report

0

let array = [] // your initial array document.body.addEventListener('click', function (e) { //when you click any element this event listner get called //then you check the target element id with the original array element id //Use filter method to filter out only those element whose id not equal to the //target id . So you will get updated array without target element array = array.filter(elem => elem.id !== e.target.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!