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

213
Views
Remove a specific element from among several elements of the same class when pressing the button on the same element
let add = document.querySelector(".add")
add.onclick = function () {
    let input = document.querySelector(".input").value;
    let newTask = document.querySelector(".tasks")
    let newTxt = document.createElement("div")
    newTxt.className = "newDiv"
    let newP = document.createTextNode(input)
    newP.className = "newInput"
    let deletButton = document.createElement("button")
    let deletTxt = document.createTextNode("Delet")
    deletButton.className = "deletButton"
    deletButton.appendChild(deletTxt)
    newTxt.appendChild(newP)
    newTxt.appendChild(deletButton)
    newTask.appendChild(newTxt)
    deletButton.onclick = function () {
        removeEle = document.getElementsByClassName(".newDiv")
        removeEle.remove(removEle)
    }
}

I create several divs with the same class name with the delete button for each div When I want to delete a particular div I select it using querySelector

But the first element is always deleted, not the div I want to delete

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

With the below statement you are getting the first .newDiv,

document.getElementsByClassName(".newDiv")

and correspondingly you are removing it.

You should make use of closures and remove newTxt since it is already available at time of your event handler declaration.

  deletButton.onclick = function(){
    newTxt.parentNode.remove(newTxt)
}

about 4 years ago · Juan Pablo Isaza Report

0

document.getElementsByClassName(".newDiv")

The above line of code will return the fist element with class name newDiv and that is the reason why your code deletes the first element.

So in order to delete the element that got clicked you have to select the button that got clicked and delete its parent.

deletButton.onclick = function(){
    this.parentNode.remove();
}
about 4 years ago · Juan Pablo Isaza Report

0

document.getElementsByClassName will always returns an array-like object of all child elements which have all of the given class name(s).

This does not have any connection with the row from which you chose to delete the item, this involves all the element having the specified class name.

Also element.remove invokation is wrong.

You can select the row from which the delete event was triggerd by selecting the closest element with the class name newDiv from the target element of the click event.

Working Fiddle

let add = document.querySelector(".add")
add.onclick = function () {
    let input = document.querySelector(".input").value;
    let newTask = document.querySelector(".tasks")
    let newTxt = document.createElement("div")
    newTxt.className = "newDiv"
    let newP = document.createTextNode(input)
    newP.className = "newInput"
    let deletButton = document.createElement("button")
    let deletTxt = document.createTextNode("Delet")
    deletButton.className = "deletButton"
    deletButton.appendChild(deletTxt)
    newTxt.appendChild(newP)
    newTxt.appendChild(deletButton)
    newTask.appendChild(newTxt)
    deletButton.onclick = function (e) {
        removeEle = e.target.closest('.newDiv');
        removeEle.remove()
    }
}
<div class="tasks"></div>
<input type="text" class="input">
<button class="add">Add</button>

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!