Getting an HTML element by ID with JavaScript
Published on 2024-10-24 • Modified on 2024-10-24
This snippet shows how to get an HTML element by ID with JavaScript. That's probably one of the most used and valuable JavaScript functions! The following example shows how to get the body
tag thanks to its body-strangebuzz
. Then, we remove this tag from the DOM, leaving a blank page! (hit F5 to reload).
// html => <body lang="en" dir="ltr" id="body-strangebuzz">
const body = document.getElementById('body-strangebuzz');
body.remove();
HTML demo of the snippet
» Click here to delete the body of this page! «
More on Stackoverflow Read the doc Random snippet