javasript Javascript variable not being constant?
Go to solution
Solved by C2dan88,
This is because ${hash} is only set for when the image is added. ${hash} wont be set when the onclick event handler is triggered when you click the X.
You should use the event object passed to the onclick handler (e) to remove the parentElement instead
document.querySelector(`[close="a${hash}"]`).addEventListener('click',function(e) {
e.target.parentElement.remove() // remove the parent element for X button that was clicked
}, { once: true }) // remove the click event automatically once clicked

Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now