Objective. Students select DOM elements using querySelector and querySelectorAll, modify element content and attributes, create and append new elements, and attach event listeners to respond to user interactions.
This lesson, its worked examples and its practice questions has not yet been verified by an industry-certified professional or by a certification or approval body. It is a study draft and may contain errors. Do not use it as instructions for work on a job site or on live equipment. Always follow your instructor, your supervising professional, and current official references and regulations.
VoTechWorks program approval and accreditation are still in progress. Credentials are issued only by the certifying bodies named in each program, after their own exams.
When a browser loads an HTML file, it parses the markup and builds a tree of objects in memory called the Document Object Model (DOM). Every HTML element becomes a node in that tree. JavaScript can read and change those nodes at any time, so the page can update without reloading. This is the foundation of all interactive web applications.
The two most useful selection methods are `document.querySelector` and `document.querySelectorAll`.
Always check that a result is not `null` before acting on it. Calling a method on `null` throws a `TypeError` that stops your script.
Once you have an element reference, you can change what users see:
You can build new elements entirely in JavaScript:
```js
const li = document.createElement('li'); // create the node
li.textContent = 'Buy groceries'; // set its text safely
document.querySelector('#task-list').appendChild(li); // add to the page
```
`appendChild` inserts the new node as the last child of the target. `prepend` inserts it as the first child. `insertAdjacentElement` gives even finer control.
Events are signals the browser sends when the user or the page does something — clicks a button, submits a form, presses a key, or the page finishes loading.
Attach a listener with `addEventListener`:
```js
const btn = document.querySelector('#add-btn');
btn.addEventListener('click', function (event) {
console.log('Button clicked');
});
```
The first argument is the event type string (`'click'`, `'submit'`, `'keydown'`, etc.). The second argument is a callback function that receives an event object. The event object carries useful information: `event.target` is the element that triggered the event, and for forms, `event.preventDefault()` stops the default browser action (such as reloading the page on submit).
When an event fires on a nested element, it does not stay there. By default, it bubbles upward through every ancestor node until it reaches the document root. So a click on a `<button>` inside a `<li>` inside a `<ul>` triggers click handlers on all three elements.
Capturing is the reverse phase: before bubbling, the browser travels down from the root to the target, notifying any listeners registered with `{capture: true}`.
This behavior sometimes causes conflicts — for example, a click on a delete button also fires the list item's click handler. Calling `event.stopPropagation()` inside a handler prevents the event from traveling further, solving the conflict.
Cross-Site Scripting is consistently listed among the most common and harmful web vulnerabilities. The rule is simple: never pass user-controlled text to `innerHTML`, `document.write`, or any property that interprets HTML. Use `textContent` or `createTextNode` instead. This is not optional — it is a professional and ethical responsibility.
The Elements panel lets you inspect the live DOM tree. Click any element to see its current attributes and computed styles. The Console panel lets you run JavaScript interactively and read error messages. When your code does not behave as expected, open DevTools first and read the error message — it tells you the file name and line number.
There are no electrical or physical hazards in this lab. Professional safety in software work means protecting users from harm:
Answer each question, then check it. Nothing is recorded; this is your own practice.
A free tech program — complimentary, as our commitment to get America working. Help us build VoTechWorks. Tell us what works, what doesn’t, and what you want to learn.
Does the school offer financial aid?
No. We don’t offer financial aid because we’re free. No, really: completely free.
Do you offer industry certification?
No. We are not certified yet, so we can’t offer that to students. What we do offer is knowledge and understanding of a trade.
Enroll first in the trade you’re interested in. You get your key, your program opens, and you begin studying.
Enroll free →