Skip to Content
DOM Enlightenment
book

DOM Enlightenment

by Cody Lindley
February 2013
Intermediate to advanced
178 pages
3h 2m
English
O'Reilly Media, Inc.
Content preview from DOM Enlightenment

Chapter 4. Element Node Selection

4.1 Selecting a Specific Element Node

The most common methods for getting a reference to a single element node are:

  • querySelector()

  • getElementById()

In the following code, I leverage both of these methods to select an element node from the HTML document.

Live code

<!DOCTYPE html>
<html lang="en">
<body>

<ul>
<li>Hello</li>
<li>big</li>
<li>bad</li>
<li id="last">world</li>
</ul>

<script>

console.log(document.querySelector('li').textContent); //logs Hello
console.log(document.getElementById('last').textContent); //logs world

</script>
</body>
</html>

The getElementById() method is pretty simple compared to the more robust querySelector() method. The querySelector() method permits a parameter in the form of a CSS selector syntax. Basically, you can pass this method a CSS3 selector (e.g., #score>tbody>tr>td:nth-of-type(2)), which it will use to select a single element in the DOM.

Notes

querySelector() will return the first node element found in the document based on the selector. For example, in the preceding code, I pass a selector that will select all the <li> elements in CSS, but only the first one is returned.

querySelector() is also defined on element nodes. This allows the method to limit its results to a specific vein of the DOM tree, thereby enabling context quering.

4.2 Selecting/Creating a List (a.k.a. NodeList) of Element Nodes

The most common methods for selecting/creating a list of nodes in an HTML document are:

  • querySelectorAll()

  • getElementsByTagName() ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Working with the DOM

Working with the DOM

Todd Brown
Developing Web Components

Developing Web Components

Jarrod Overson, Jason Strimpel
HTML5 and CSS3: Building Responsive Websites

HTML5 and CSS3: Building Responsive Websites

Thoriq Firdaus, Ben Frain, Benjamin LaGrone
The Human Factor in AI-Based Decision-Making

The Human Factor in AI-Based Decision-Making

Philip Meissner, Christoph Keding

Publisher Resources

ISBN: 9781449344498Errata Page