<p>In <ahref="https://www.digitalocean.com/community/tutorials/understanding-the-dom-tree-and-nodes">Understanding the DOM Tree and Nodes</a>, we went over how the DOM is structured as a tree of objects called nodes, and that nodes can be text, comments, or elements. Usually when we access content in the DOM, it will be through an HTML element node.</p>
<p>In order to be confident in accessing elements in the DOM, it’s good to have a working knowledge of CSS selectors, syntax and terminology as well as an understanding of HTML elements. In this tutorial, you will learn several ways to access elements in the DOM: by ID, class, tag, and query selectors.</p>
<h2id="overview">Overview</h2>
<p>Here is a table overview of the five methods we will cover in this tutorial.</p>
<table>
<thead>
<tr>
<th>Gets</th>
<th>Selector Syntax</th>
<th>Method</th>
</tr>
</thead>
<tbody>
<tr>
<td>ID</td>
<td><code>#demo</code></td>
<td><code>getElementById()</code></td>
</tr>
<tr>
<td>Class</td>
<td><code>.demo</code></td>
<td><code>getElementsByClassName()</code></td>
</tr>
<tr>
<td>Tag</td>
<td><code>demo</code></td>
<td><code>getElementsByTagName()</code></td>
</tr>
<tr>
<td>Selector (single)</td>
<td></td>
<td><code>querySelector()</code></td>
</tr>
<tr>
<td>Selector (all)</td>
<td></td>
<td><code>querySelectorAll()</code></td>
</tr>
</tbody>
</table>
<p>It is helpful when studying the DOM to work with the examples on your own to ensure that you are understanding and retaining the information you learn.</p>
<p>Create a new file, <code>access.html</code>, in your own project to work through the examples along with this article. If you are unsure how to work with JavaScript and HTML locally, review our <ahref="https://www.digitalocean.com/community/tutorials/how-to-add-javascript-to-html">How To Add JavaScript to HTML</a> tutorial.</p>
<spanclass="p"><</span><spanclass="nt">title</span><spanclass="p">></span>Accessing Elements in the DOM<spanclass="p"></</span><spanclass="nt">title</span><spanclass="p">></span>
<spanclass="p"><</span><spanclass="nt">h1</span><spanclass="p">></span>Accessing Elements in the DOM<spanclass="p"></</span><spanclass="nt">h1</span><spanclass="p">></span>
<spanclass="p"><</span><spanclass="nt">div</span><spanclass="na">id</span><spanclass="o">=</span><spanclass="s">"demo"</span><spanclass="p">></span>Access me by ID<spanclass="p"></</span><spanclass="nt">div</span><spanclass="p">></span>
<spanclass="p"><</span><spanclass="nt">div</span><spanclass="na">class</span><spanclass="o">=</span><spanclass="s">"demo"</span><spanclass="p">></span>Access me by class (1)<spanclass="p"></</span><spanclass="nt">div</span><spanclass="p">></span>
<spanclass="p"><</span><spanclass="nt">div</span><spanclass="na">class</span><spanclass="o">=</span><spanclass="s">"demo"</span><spanclass="p">></span>Access me by class (2)<spanclass="p"></</span><spanclass="nt">div</span><spanclass="p">></span>
<spanclass="p"><</span><spanclass="nt">article</span><spanclass="p">></span>Access me by tag (1)<spanclass="p"></</span><spanclass="nt">article</span><spanclass="p">></span>
<spanclass="p"><</span><spanclass="nt">article</span><spanclass="p">></span>Access me by tag (2)<spanclass="p"></</span><spanclass="nt">article</span><spanclass="p">></span>
<spanclass="p"><</span><spanclass="nt">div</span><spanclass="na">id</span><spanclass="o">=</span><spanclass="s">"demo-query"</span><spanclass="p">></span>Access me by query<spanclass="p"></</span><spanclass="nt">div</span><spanclass="p">></span>
<spanclass="p"><</span><spanclass="nt">div</span><spanclass="na">class</span><spanclass="o">=</span><spanclass="s">"demo-query-all"</span><spanclass="p">></span>Access me by query all (1)<spanclass="p"></</span><spanclass="nt">div</span><spanclass="p">></span>
<spanclass="p"><</span><spanclass="nt">div</span><spanclass="na">class</span><spanclass="o">=</span><spanclass="s">"demo-query-all"</span><spanclass="p">></span>Access me by query all (2)<spanclass="p"></</span><spanclass="nt">div</span><spanclass="p">></span>
<p>In this HTML file, we have many elements that we will access with different <code>document</code> methods. When we render the file in a browser, it will look similar to this:</p>
<p><imgalt="Browser rendering of access.html page"src="https://assets.digitalocean.com/articles/eng_javascript/dom/html-rendering.png"/></p>
<p>We’ll be using the different methods that we outlined in the <ahref="https://www.digitalocean.com/community/tutorials/how-to-access-elements-in-the-dom#overview">Overview</a> above to access the available elements in the file.</p>
<h2id="accessing-elements-by-id">Accessing Elements by ID</h2>
<p>The easiest way to access a single element in the DOM is by its unique <ahref="https://developer.mozilla.org/en-US/docs/Web/API/Element/id">ID</a>. You can get an element by ID with the <ahref="https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementById"><code>getElementById()</code></a> method of the <code>document</code> object.</p>
<p>In order to be accessed by ID, the HTML element must have an <code>id</code> attribute. You have a <code>div</code> element with an ID of <code>demo</code> you can use:</p>
<divclass="codehilite"><pre><span></span><code><spanclass="p"><</span><spanclass="nt">div</span><spanclass="na">id</span><spanclass="o">=</span><spanclass="s">"demo"</span><spanclass="p">></span>Access me by ID<spanclass="p"></</span><spanclass="nt">div</span><spanclass="p">></span>
</code></pre></div>
<p>In the <em>Console</em>, get the element and assign it to the <code>demoId</code> variable.</p>
<spanclass="err">!</span><spanclass="o">[</span><spanclass="n">Browser rendering of ID element styling</span><spanclass="o">]</span><spanclass="p">(</span><spanclass="nl">https</span><spanclass="p">:</span><spanclass="o">//</span><spanclass="n">assets</span><spanclass="p">.</span><spanclass="n">digitalocean</span><spanclass="p">.</span><spanclass="n">com</span><spanclass="o">/</span><spanclass="n">articles</span><spanclass="o">/</span><spanclass="n">eng_javascript</span><spanclass="o">/</span><spanclass="n">dom</span><spanclass="o">/</span><spanclass="n">id</span><spanclass="o">-</span><spanclass="k">element</span><spanclass="p">.</span><spanclass="n">png</span><spanclass="p">)</span><spanclass="w"></span>
<p>Now we want to access more than one element, and in our example we have two elements with a <code>demo</code> class.</p>
<divclass="codehilite"><pre><span></span><code><spanclass="p"><</span><spanclass="nt">div</span><spanclass="na">class</span><spanclass="o">=</span><spanclass="s">"demo"</span><spanclass="p">></span>Access me by class (1)<spanclass="p"></</span><spanclass="nt">div</span><spanclass="p">></span>
<spanclass="p"><</span><spanclass="nt">div</span><spanclass="na">class</span><spanclass="o">=</span><spanclass="s">"demo"</span><spanclass="p">></span>Access me by class (2)<spanclass="p"></</span><spanclass="nt">div</span><spanclass="p">></span>
<divclass="codehilite"><pre><span></span><code><spanclass="p">[</span><spanclass="n">JavaScript</span><spanclass="w"></span><spanclass="n">arrays</span><spanclass="p">](</span><spanclass="n">https</span><spanclass="o">:</span><spanclass="c1">//www.digitalocean.com/community/tutorials/understanding-arrays-in-javascript) must be accessed with an index number. You can change the first element of this array by using an index of `0`.</span>
<p>Generally when accessing elements by class, we want to apply a change to all the elements in the document with that particular class, not just one. You can do this by creating a <code>for</code> loop, and looping through every item in the array. </p>
<p>Just like accessing an element by its class, <code>getElementsByTagName()</code> will return an array-like object of elements, and you can modify every tag in the document with a <code>for</code> loop.</p>
<spanclass="k">If</span><spanclass="w"></span><spanclass="n">you</span><spanclass="w"></span><spanclass="n">have</span><spanclass="w"></span><spanclass="k">any</span><spanclass="w"></span><spanclass="n">experience</span><spanclass="w"></span><spanclass="k">with</span><spanclass="w"></span><spanclass="n">the</span><spanclass="w"></span><spanclass="err">[</span><spanclass="n">jQuery</span><spanclass="err">]</span><spanclass="p">(</span><spanclass="n">https</span><spanclass="o">://</span><spanclass="n">jquery</span><spanclass="p">.</span><spanclass="n">com</span><spanclass="o">/</span><spanclass="p">)</span><spanclass="w"></span><spanclass="n">API</span><spanclass="p">,</span><spanclass="w"></span><spanclass="n">you</span><spanclass="w"></span><spanclass="n">may</span><spanclass="w"></span><spanclass="n">be</span><spanclass="w"></span><spanclass="n">familiar</span><spanclass="w"></span><spanclass="k">with</span><spanclass="w"></span><spanclass="n">jQuery</span><spanclass="s1">'s method of accessing the DOM with CSS selectors.</span>
<spanclass="s1">```js</span>
<spanclass="s1">$('</span><spanclass="c1">#demo'); // returns the demo ID element in jQuery</span><spanclass="w"></span>
<p>To access a single element, you can use the <ahref="https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector"><code>querySelector()</code></a> method. In our HTML file, we have a <code>demo-query</code> element</p>
<divclass="codehilite"><pre><span></span><code><spanclass="p"><</span><spanclass="nt">div</span><spanclass="na">id</span><spanclass="o">=</span><spanclass="s">"demo-query"</span><spanclass="p">></span>Access me by query<spanclass="p"></</span><spanclass="nt">div</span><spanclass="p">></span>
<p>The selector for an <code>id</code> attribute is the hash symbol (<code>#</code>). You can assign the element with the <code>demo-query</code> id to the <code>demoQuery</code> variable.</p>
<divclass="codehilite"><pre><span></span><code>In the case of a selector with multiple elements, such as a class or a tag, `querySelector()` will return the first element that matches the query. You can use the [`querySelectorAll()`](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll) method to collect all the elements that match a specific query.
<spanclass="nt"><div</span><spanclass="na">class=</span><spanclass="s">"demo-query-all"</span><spanclass="nt">></span>Access me by query all (1)<spanclass="nt"></div></span>
<spanclass="nt"><div</span><spanclass="na">class=</span><spanclass="s">"demo-query-all"</span><spanclass="nt">></span>Access me by query all (2)<spanclass="nt"></div></span>
<p>The selector for a <code>class</code> attribute is a period or full stop (<code>.</code>), so you can access the class with <code>.demo-query-all</code>.</p>
<p><imgalt="Browser rendering of querySelector() styling"src="https://assets.digitalocean.com/articles/eng_javascript/dom/query-selector.png"/></p>
<p>With <code>querySelector()</code>, comma-separated values function as an OR operator. For example, <code>querySelector('div, article')</code> will match <code>div</code><em>or</em><code>article</code>, whichever appears first in the document. With <code>querySelectorAll()</code>, comma-separated values function as an AND operator, and <code>querySelectorAll('div, article')</code> will match all <code>div</code><em>and</em><code>article</code> values in the document.</p>
<p>Using the query selector methods is extremely powerful, as you can access any element or group of elements in the DOM the same way you would in a CSS file. For a complete list of selectors, review <ahref="https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors">CSS Selectors</a> on the Mozilla Developer Network.</p>
<p>Below is the complete script of the work you did above. You can use it to access all the elements on our example page. Save the file as <code>access.js</code> and load it in to the HTML file right before the closing <code>body</code> tag.</p>
<spanclass="p"><</span><spanclass="nt">title</span><spanclass="p">></span>Accessing Elements in the DOM<spanclass="p"></</span><spanclass="nt">title</span><spanclass="p">></span>
<spanclass="p"><</span><spanclass="nt">h1</span><spanclass="p">></span>Accessing Elements in the DOM<spanclass="p"></</span><spanclass="nt">h1</span><spanclass="p">></span>
<spanclass="p"><</span><spanclass="nt">div</span><spanclass="na">id</span><spanclass="o">=</span><spanclass="s">"demo"</span><spanclass="p">></span>Access me by ID<spanclass="p"></</span><spanclass="nt">div</span><spanclass="p">></span>
<spanclass="p"><</span><spanclass="nt">div</span><spanclass="na">class</span><spanclass="o">=</span><spanclass="s">"demo"</span><spanclass="p">></span>Access me by class (1)<spanclass="p"></</span><spanclass="nt">div</span><spanclass="p">></span>
<spanclass="p"><</span><spanclass="nt">div</span><spanclass="na">class</span><spanclass="o">=</span><spanclass="s">"demo"</span><spanclass="p">></span>Access me by class (2)<spanclass="p"></</span><spanclass="nt">div</span><spanclass="p">></span>
<spanclass="p"><</span><spanclass="nt">article</span><spanclass="p">></span>Access me by tag (1)<spanclass="p"></</span><spanclass="nt">article</span><spanclass="p">></span>
<spanclass="p"><</span><spanclass="nt">article</span><spanclass="p">></span>Access me by tag (2)<spanclass="p"></</span><spanclass="nt">article</span><spanclass="p">></span>
<spanclass="p"><</span><spanclass="nt">div</span><spanclass="na">id</span><spanclass="o">=</span><spanclass="s">"demo-query"</span><spanclass="p">></span>Access me by query<spanclass="p"></</span><spanclass="nt">div</span><spanclass="p">></span>
<spanclass="p"><</span><spanclass="nt">div</span><spanclass="na">class</span><spanclass="o">=</span><spanclass="s">"demo-query-all"</span><spanclass="p">></span>Access me by query all (1)<spanclass="p"></</span><spanclass="nt">div</span><spanclass="p">></span>
<spanclass="p"><</span><spanclass="nt">div</span><spanclass="na">class</span><spanclass="o">=</span><spanclass="s">"demo-query-all"</span><spanclass="p">></span>Access me by query all (2)<spanclass="p"></</span><spanclass="nt">div</span><spanclass="p">></span>
<p>You can continue to work on these template files to make additional changes by accessing HTML elements.</p>
<h2id="conclusion">Conclusion</h2>
<p>In this tutorial, we went over 5 ways to access HTML elements in the DOM — by ID, by class, by HTML tag name, and by selector. The method you will use to get an element or group of elements will depend on browser support and how many elements you will be manipulating. You should now feel confident to access any HTML element in a document with JavaScript through the DOM.</p>