DOM: What Does Live Object Mean?

By Xah Lee. Date: . Last updated: .

JavaScript DOM Methods such as document.getElementsByClassName() returns a live NodeList or HTMLCollection.

What does live object mean?

the DOM spec says:

If a collection is live, then the attributes and methods on that object must operate on the actual underlying data, not a snapshot of the data.

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

For example, if you use es = document.getElementsByTagName("p") and use for-loop by checking es.length, and for each iteration you add a new "p" element, it'll be a infinite loop, because the number of items (the es.length) also gets updated.

Here's another example. When you get a element of “h6”, if there's none, but if later a h6 element is inserted to the page, your previous get result magically contains the item.

Evaluate the following line by line in your browser console. [see JS: How to Use Browser Console]

// demo of live object

const x = document .getElementsByTagName("h6");

x.length // is 0

// insert a h6 tag into document body
document .body.appendChild(document .createElement("h6"));

// our selection of h6 got updated
x.length // is 1

Note: not all get element methods return a live object. {document.getElementById(), document.querySelectorAll()} return non-live object.

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

[see DOM: NodeList vs HTMLCollection]

How to Think About Live Object?

Live object is not intuitive. You can think of it as delayed evaluation or lazy evaluation. Method or property of live object is re-computed when their result is accessed.

What's the Use of Live Object?

One use, is that you can bind data to elements in charts, such as stock quotes. When the data changes, the plot automatically changes. [see Practical SVG Tutorial]

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

DOM HTML GUI

  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 window.open
  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

jQuery

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

node.js

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