Manipulating web pages with JavaScript, object-based programming, and form validation
Grade XII • Computer Science ⏱️ ~40 minBrief Intro — JS DOM, Objects, Events & Forms
JavaScript is not just for math and logic — it REACHES INTO your web page and changes it live. Through the Document Object Model (DOM), JavaScript can modify HTML elements, handle user events, and validate form inputs before they reach the server.
In this activity, you'll learn how the browser represents your page as a tree of objects, how to select and modify elements, how to work with JavaScript objects, and how to respond to user interactions like clicks and form submissions.
The Document Object Model is the bridge between HTML and JavaScript.
Task 1: What is the HTML DOM?
What is the Document Object Model and how does the browser use it?
Answer: The HTML DOM is a standard object model where the browser creates a tree of objects representing the page, allowing JavaScript to get, change, add, or delete HTML elements.
Task 2: Selecting & Changing Elements
How do you select and modify HTML elements using JavaScript?
Answer: Use getElementById(id) to select elements and innerHTML to change their content. The DOM allows JavaScript to modify pages dynamically.
JavaScript is object-based — objects are containers for named values (properties) and actions (methods).
Task 3: JavaScript Objects
What are JavaScript objects and how do you create them?
Answer: JavaScript objects hold properties and methods. Create them with object literals or new keyword. Primitives (string, number, boolean, null, undefined) should not be created as objects.
Task 4: Object Properties & Methods
What are object properties and methods, and how do you access them?
Answer: Properties are named values; methods are actions. Access properties with dot or bracket notation. Access methods with parentheses. The this keyword refers to the object owning the function.
Task 5: Working with Object Members
How do you use object members in a web page?
Answer: Object members can be accessed and used to manipulate page content. Build objects with properties and methods, then use DOM methods to display their data.
Events allow JavaScript to react to user interactions like clicks, page loads, and form submissions.
Use the controls below to manipulate the embedded page and see JavaScript modify the DOM in real-time.
This paragraph can be changed with JavaScript
Task 6: HTML DOM Events
What are HTML events and how does JavaScript respond to them?
Answer: Events are actions that occur (click, load, change). JavaScript can execute code when events happen using event attributes like onclick, onload, onchange.
Task 7: Changing Images with Events
How do you change an image source using JavaScript events?
Answer: Use onclick to trigger a function that modifies the src attribute of an img element. The DOM allows dynamic image swapping through JavaScript.
Task 8: Predicting Event Behaviour
Which handler fires for which event?
Answer: onclick triggers on clicks, onload on page/image load, onchange on input changes, onmouseover on mouse hover. Each event type has a specific handler.
Client-side validation ensures user input is correct before sending it to the server.
Task 9: Why Validate in the Browser?
What is form validation and why do it on the client side?
Answer: Validation ensures correct input. Client-side validation happens in the browser before submission for immediate feedback. Server-side validation happens after submission for final verification.
Task 10: Hands-on Form Validation
Validate a name and password field in a form.
Answer: Use document.forms to access input values, test conditions (name not empty, password >= 6 chars), alert user if validation fails, and return false to prevent form submission.
Task 11: Validating Before Submission
How does form validation work with the submit event?
Answer: Submit-time validation runs on submit event and prevents submission if validation fails. Field-time validation provides immediate feedback. HTML5 required attribute provides automatic validation.
Combining DOM, objects, and events to create interactive web experiences.
Task 12: Combine DOM + Object + Event
Build a person object and use its method to change page content on button click.
Answer: Create objects with properties and methods. Use event handlers to call object methods. Use DOM methods to update page content. This demonstrates full integration of JavaScript concepts.
Ready to test your knowledge?
Take the Assessment →