Reputation: 683
Currently, I am making a password storing website. Part of the UI is when you double click on a password item it gives you the option to delete it.
Here is my event listner (with function), myPasswordScreenID
is the parent element to all the classes with passwordItem
.
document.getElementById('myPasswordsScreenID').addEventListener('dbclick', function(event) {
if (event.target.classList.contains('passwordItem')) {
if (confirm('Would you like to delete this item? It will be gone forever!')) {
event.target.style.display = 'none';
event.target.style.margin = 0;
localStorage.removeItem(event.target.id);
}
}
}, false);
For some reason if I use the event listener 'click' instead of 'dbclick' it works? I have no clue why. Any help would be greatly appreciated.
Upvotes: 0
Views: 447
Reputation: 5011
In addition to @AndreaOggioni's answer, using the code from MDN:
const element = document.getElementById('myPasswordsScreenID');
element.addEventListener('dblclick', function(event) {
if (
event.target.classList.contains('passwordItem') &&
confirm('Would you like to delete this item? It will be gone forever!')
) {
event.target.style.display = 'none';
event.target.style.margin = 0;
localStorage.removeItem(event.target.id);
}
});
Good luck.
Upvotes: 0