DOM: Get Elements by ID, Tag, Name, Class, CSS Selector

By Xah Lee. Date: . Last updated: .

Get Current Script Element

document .currentScript → return the current script element.

[see DOM: Get Current Script Tag]

Get Element by Matching the Value of the 「id」 Attribute


Return a non-live element object. Returns null if not found.

const elem = document .getElementById("xyz");"green";

Note: element id must be unique per HTML file. (embeded iframe counts as different HTML file.)

Get Elements by Tag Name


Return a live HTMLCollection.

The tag_name is "div", "span", "p", etc.

const pp = document .getElementsByTagName("p");

Array.from ( pp ) .forEach (((x) => { = "green"; }) );

Get Elements by Matching the Value of the 「class」 Attribute


Return a live HTMLCollection.

The class_values can be multiple classes separated by space. For example: "aa bb", and it'll get elements, where each element is in both class “aa” and “bb”.

const xx = document .getElementsByClassName("js");
Array.from ( xx ). forEach (((x) => { = "green"; }));

Note: a HTML element can belong to more than one class. Here's a example:

<p class="aa">1</p>

<p class="bb">2</p>

<p class="bb aa">3</p>

<p class="bb cc aa">4</p>

<script>document.getElementsByClassName("aa bb");</script>

<!-- it'll get 3 and 4. -->

Get Elements by Matching the Value of the 「name」 Attribute


Return a live NodeList, of all elements that have the name=val attribute and value pair.

Here's a sample HTML:

<p name="abc">1</p>
<div class="zz" name="xyz">2</div>
<div class="zz" name="xyz">3</div>

<input name="xyz" type="text" size="20">

Here's JavaScript code that makes all elements with name="xyz" green.

const xyz = document .getElementsByName("xyz");

Array.from ( xyz ).forEach (((x) => {"green"; }) );

Get Elements using CSS Selector Syntax


Return a non-live first element that match the CSS selector css_selector. The css_selector is a string of CSS syntax, and can be several selectors separated by comma.


Return a non-live NodeList, of elements that match the CSS selector css_selector. The css_selector is a string of CSS syntax, and can be several selectors separated by comma.

const xx = document .querySelectorAll("p");
Array.from (xx) .forEach ( ((x) => {"green"; }));

The syntax for the argument is the same as CSS selector syntax. For example:

[see CSS Selector Syntax]

What's Live Object?

DOM: What Does Live Object Mean?

HTMLCollection vs NodeList?

DOM: NodeList vs HTMLCollection

Array-Like Object

Array Methods

JS: Array.prototype

Web Scripting Overview

  1. Browser Window Object, DOM
  2. Intro to Event-Based Programing
  3. JavaScript Load Order

DOM How-To

  1. Basic DOM Methods
  2. Get Element by ID, Tag, Name, Class, CSS
  3. Change CSS
  4. Change Node Content
  5. Create/Insert Node
  6. Remove Node
  7. Get Attribute Value
  8. Set Attribute Value
  9. Remove Attribute
  10. List/Add/Remove Class Attribute
  11. Add/Remove Event Handler
  12. Navigate DOM Tree
  13. Node Type, Name, Value


  1. Button
  2. Input Text Field
  3. Password Text Field
  4. Textarea
  5. Radio Button
  6. Checkbox
  7. Selection, Popup Menu
  8. Input Range Slider
  9. Color Picker

Web Scripting Examples

  1. Disable Right Click
  2. document.write
  3. Image Rollover
  4. Pop-up New Window
  5. Digital Clock
  6. Stopwatch
  7. Fade a Element
  8. How to Create Tooltip
  9. Falling Snow Effect
  10. Box Following Scroll

Web Scripting Misc

  1. Web Cookies
  2. Web Storage
  3. DOM: Open URL
  4. Encode URL, Escape String
  5. Browser Info, Navigator Object
  6. Get URL (window.location)
  7. Find Window Width
  8. Find Element Width
  9. WebSocket
  10. Value of “this” in Event Handler
  11. Event Delegation
  12. Get Current Script Tag
  13. insertAfter Element
  14. Randomize List
  15. create Document Fragment
  16. innerHTML, nodeValue, textContent, innerText?
  17. What is Live Object
  18. NodeList vs HTMLCollection
  19. Whitespace Nodes


  1. jQuery Basics
  2. jQuery() vs querySelectorAll()
  3. Write JQuery Plugin


Like it? Help me by telling your friends. Or, Put $5 at patreon.

Or, Buy JavaScript in Depth

If you have a question, put $5 at patreon and message me.

Web Dev Tutorials

  1. HTML
  2. Visual CSS
  3. JS in Depth
  4. JS Object Ref
  5. DOM Scripting
  6. SVG
  7. Blog