Here's one possible solution:
let target = {}
...
container.addEventListener('click', (e) => {
if (e.target.className === 'box') {
document.querySelector('#color').innerHTML =
e.target.style.backgroundColor
document.querySelector('#message').innerHTML = e.target.innerHTML
messageBox.style.visibility = 'visible'
target = e.target
}
})
document.querySelector('#delete').addEventListener('click', (event) => {
messageBox.style.visibility = 'hidden'
target.remove()
})
You can find this solution on GitHub at https://github.com/PacktPublishing/Hands-on-JavaScript-for-Python-Developers/blob/master/chapter-7/stickies/solution-code-3/script.js.
While this uses a global variable, it's still more efficient. By encapsulating our whole program in a function or class, we could eliminate the global variable, but that's not important for this concept...