DOM Recipes: DOM timing and when elements are ready

Join the AI Workshop to learn more about AI and how it can be applied to web development. Next cohort February 1st, 2026

The AI-first Web Development BOOTCAMP cohort starts February 24th, 2026. 10 weeks of intensive training and hands-on projects.


There’s one thing that might not be apparent at first when working with the DOM: timing.

When you access the value of a DOM element and you store it into a variable, that variable is NOT going to be updated with the new value when the DOM element changes.

Suppose you have an input field in a form <input id="temperature">, and you get its value in this way:

const temperature = document.querySelector('input#temperature').value

The temperature variable gets the value of the state of the input field at the moment the browser executes this statement, and then the value stays the same forever.

This is why you can’t do like this:

const temperature = document.querySelector('input#temperature').value

document.querySelector('form')
        .addEventListener('submit', event => {
  //send the temperature value to your server
})

but you need to access the temperature value when you submit the form:

document.querySelector('form')
        .addEventListener('submit', event => {
  const temperature = document.querySelector('input#temperature').value
  //send the temperature value to your server
})

Alternatively you can store the input field reference in a variable, and use that to access its value at submit:

const temperatureElement = document.querySelector('input#temperature')
document.querySelector('form')
        .addEventListener('submit', event => {
  const temperature = temperatureElement.value
  //send the temperature value to your server
})

Lessons in this unit:

0: Introduction
1: Hiding and showing elements
2: Check if element is descendant
3: Change colors/styles dynamically
4: ▶︎ DOM timing and when elements are ready
5: stopPropagation vs preventDefault
6: How to detect adblockers
7: Creating exit intent popups