DOM selectors and input events
ECMAScript 2026
Select several elements at once and respond to typing, committing a value, or pressing a key.
On this page
Select many nodes with querySelectorAll()
Pass a CSS selector to get a static NodeList of all matches. For each element, use forEach(). The original list does not update if you add elements later.
How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Select many nodes with querySelectorAll()</title>
</head>
<body>
<ul><li class="product">Pen</li><li class="product">Notebook</li></ul>
<script>
const products = document.querySelectorAll(".product");
products.forEach(product => {
product.textContent += " ✓";
});
</script>
</body>
</html>
What to expect Both list items get a check mark. Add a third matching item to the HTML and run again.
Handle every edit with input
input fires as the user changes the value. Read the current value from the field to update a live counter or preview.
How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Handle every edit with input</title>
</head>
<body>
<label for="note">Order note</label>
<input id="note">
<p id="result" aria-live="polite"></p>
<script>
const note = document.querySelector("#note");
const result = document.querySelector("#result");
note.addEventListener("input", () => {
result.textContent = note.value.length + " characters";
});
</script>
</body>
</html>
What to expect Type a note and see its character count change while typing.
React to a committed value with change
For a text field, change usually fires after the edited value is committed when the field loses focus. For a select, it fires when the user chooses a new option.
How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>React to a committed value with change</title>
</head>
<body>
<label for="note">Order note</label>
<input id="note">
<p id="result" aria-live="polite"></p>
<script>
const note = document.querySelector("#note");
const result = document.querySelector("#result");
note.addEventListener("change", () => {
result.textContent = "Saved: " + note.value;
});
</script>
</body>
</html>
What to expect Type a different note, then click outside the field to see the saved value.
Read a pressed key with keydown
keydown fires when a key is pressed. event.key describes that key, so a handler can react to Enter or another particular key. For changes to text itself, listen for input.
How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Read a pressed key with keydown</title>
</head>
<body>
<label for="note">Order note</label>
<input id="note">
<p id="result" aria-live="polite"></p>
<script>
const note = document.querySelector("#note");
const result = document.querySelector("#result");
note.addEventListener("keydown", event => {
if (event.key === "Enter" && !event.isComposing) {
result.textContent = "Confirmed: " + note.value;
}
});
</script>
</body>
</html>
What to expect Focus the field, type a note, and press Enter to confirm it.