I am just starting to learn JS and HTML. I want to delete an element from the page as soon as it loads. Here's my basic HTML code:
<html>
<head>
</head>
<body>
<div id="trapParent"><a id="trap" href='/trap/removeByJS'> Javascript removal </a></div>
<script>
function timeout() {
setTimeout(function () {
timeout();
}, 1000);
}
timeout();
var parent = document.getElementById('trapParent');
var child = document.getElementById('trap');
while (child & parent){
parent.removeChild(child);
parent = undefined;
child = undefined;
}
</script>
</body>
</html>
However, when I check the page, the link is still there even though I removed it using JavaScript. Even after trying a timeout function, the link persists in the DOM. Am I overlooking something?
I included the timeout function to ensure that the script executes after the page fully loads.