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

273
Views
How do i make the button that's also created with the list item work? I want it to delete the current item of the array. Each item has it own button

I tried to create an onclick event, but nothing happened

    let myWork = []
const submitEl = document.getElementById("submit-el")
const clearEl = document.getElementById("clear-el")
const listEl = document.getElementById("list-el")
const textEl = document.getElementById("text-el")

submitEl.addEventListener("click", function () {
    myWork.push(textEl.value)
    renderItems()

})

clearEl.addEventListener("click", function () {
    myWork.pop()
    renderItems()
})

function renderItems() {
    let listItems = ""
    for (let i = 0; i < myWork.length; i++) {
        listItems += `<li>${myWork[i]} </li> <button>Done</button>`
    }
    listEl.innerHTML = listItems
}

How do I make this button delete itself?

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

If I understand your question, you want the "done" button delete its own line on click.

As a fast solution, you can write:

listItems += `<li>${myWork[i]} </li> <button onclick="deleteWorkByIndex(${i})>Done</button>`

function deleteWorkByIndex(index) {
    myWork.splice(index, 1); // Delete the item n°{index}
    renderItems();
}
about 4 years ago · Santiago Gelvez 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!