Skip to content

Instantly share code, notes, and snippets.

@nicordev
Last active May 20, 2019 06:31
Show Gist options
  • Save nicordev/0e033953f82048b925c27e09e3e0c5e2 to your computer and use it in GitHub Desktop.
Save nicordev/0e033953f82048b925c27e09e3e0c5e2 to your computer and use it in GitHub Desktop.
Hide or remove DOM elements of a page by clicking on it (left click: hide, middle + left button: remove)
var domKiller = {
hiddenElements: [],
activated: false,
/**
* Initialize and activate the remove action when clicking simultaneously on left and middle mouse button and the hide action when clicking on the left mouse button
*/
init: function () {
domKiller.activate();
document.addEventListener("click", event => {
if (domKiller.activated && event.buttons > 2) {
event.preventDefault();
event.target.remove();
} else if (domKiller.activated) {
event.preventDefault();
let targetElement = event.target;
domKiller.hiddenElements.push({
element: targetElement,
oldDisplay: targetElement.style.display
});
targetElement.style.display = "none";
}
});
},
/**
* Activate the remove action
*/
activate: function () {
domKiller.activated = true;
},
/**
* Deactivate the remove action
*/
deactivate: function () {
domKiller.activated = false;
},
/**
* Show all hidden elements
*/
revealAll: function () {
for (let hiddenElement of domKiller.hiddenElements) {
hiddenElement.element.style.display = hiddenElement.oldDisplay;
}
}
};
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div>zogzog</div>
<div>zogzog</div>
<a href="google.com">google</a>
<a href="google.com">google</a>
<a href="google.com">google</a>
<script src="domKiller.js"></script>
<script>
domKiller.init();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment