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

199
Views
js replaceChild preserve click listener or inline onclick method

I have a div that's used to display a work area for the user, and content is loaded dynamically. When trying to implement a previous/back button, my content loads, but if that content contains an onclick listener it doesn't trigger.

My work area is an empty <div id="work-area"></div>

I'm using cloneNode() and replaceChild() like so:

const workArea = document.getElementById(`work-area`)
let backup = workArea.cloneNode(true)

closeWorkArea = () => {
    workArea.innerHTML = ``
    backup = workArea.cloneNode(true)
    workArea.parentNode.replaceChild(backup, workArea)
}
previous = () => {
    workArea.parentNode.replaceChild(backup, workArea)
}

and when I load a certain page, I create a backup and load a template:

openItem = (index) => {
    backup = workArea.cloneNode(true)
    workArea.innerHTML = ``
    ...
    const itemPageCard = document.getElementById(`item-page-card`).content.cloneNode(true)
    // NOTE : I've tried both of these assignments below, neither work
    itemPageCard.children[2].addEventListener(`click`, () => { promptDelete(index) })
    itemPageCard.children[2].setAttribute(`onclick`, `promptDelete(${index})`)
    ...
    workArea.appendChild(itemPageCard)
}

The promptDelete() function loads a template which contains a button that will delete the users data, as well as a button that calls previous()

promptDelete = (index) => {
    backup = workArea.cloneNode(true)
    workArea.innerHTML = ``
    const prompt = document.getElementById(`prompt-delete`).content.cloneNode(true)
    ...
    // NOTE : Clicking this does work, and loads the previous page data
    prompt.children[2].children[0].addEventListener(`click`, () => { previous() })
    prompt.children[2].children[1].addEventListener(`click`, () => { deleteItemByIndex(index) })
    workArea.appendChild(prompt)
}

Once the previous page is loaded, the click events from the initial openItem() call no longer occur. I suspect its the workArea.parentNode.replaceChild(backup, workArea), but don't know how to restore those listeners. Even if I use the inline method setAttribute('onclick', ..) I can see the onclick="promptDelete(0)" inside the html, confirming it's cloning correctly, however still nothing

Note : I can't just use openItem() with an index in lieu of previous() because I also load other types of content into the work area

about 4 years ago · Juan Pablo Isaza
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!