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

187
Views
Is there a way to delete only clicked element when those elements all have the same ID?

I want to implement "delete comment" option when I click on the comment. However, what I have implemented here deletes comment in the order regardless of what I click and I want it to delete one that I am clicking. I tried using for loop so each one gets different ID with number next to it but it got too complicated to track when someone deletes and adds again. I would greatly appreciate any 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

I used target.parentNode, please see snippet below.

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

ids must always be unique on a page. The following shows a solution based on filtering for a common class:

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!