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();
More on Stackoverflow Read the doc Random snippet
Call to action
Did you like this post? You can help me back in several ways: (use the "reply" link on the right to comment or to contact me )
- Report any error/typo.
- Report something that could be improved.
- Like and repost!
- Follow me on Bluesky 🦋
- Subscribe to the RSS feed.
- Click on the More on Stackoverflow buttons to make me win "Announcer" badges 🏅.
Thank you for reading! And see you soon on Strangebuzz! 😉