Select a DOM element with getElementByID

theianjones

Ian Jones

Posted on February 20, 2020

Select a DOM element with getElementByID

If you would prefer to watch this post, you can do so with this community resource lesson on egghead.io.

Getting an element by id is so common that the document object has a helper function for us to use.

Given this html:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>Select an Element with document.getElementById</title>
  </head>
  <body>
    <ul>
      <li id="movie-1">Peanut Butter Falcon</li>
      <li id="movie-2">Knives Out</li>
    </ul>
  </body>
</html>

We can grab our <li id="movie-2">Knives Out</li> by calling document.getElementById('movie-2').

That's it!

💖 💪 🙅 🚩
theianjones
Ian Jones

Posted on February 20, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

WTF is the DOM?
javascript WTF is the DOM?

February 18, 2020

Select a DOM element with getElementByID
javascript Select a DOM element with getElementByID

February 20, 2020