Selecting and Manipulating DOM Elements
The Document Object Model (DOM) provides an interface for HTML documents. Developers use specific methods to locate elements based on identifiers, tags, or classes to modify their properties dynamically.
Access Methods Overview
- getElementById: Returns a single element node based on its unique
idattribute. - getElementsByTagName: Returns a live NodeList of all elements with the specified tag name.
- getElementsByName: Returns a NodeList of elements sharing the same
nameattribute value. - getElementsByClassName: Returns a live NodeList of elements matching the provided class name.
Implementation Example
The following code demonstrates accessing elements and applying changes such as text updates, style modifications, and state toggles.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>DOM Traversal Demo</title>
</head>
<body>
<img id="target-image" src="image-placeholder.jpg"><br>
<div class="content-box">Section One</div>
<div class="content-box">Section Two</div>
<input type="checkbox" name="user-prefs"> Option A
<input type="checkbox" name="user-prefs"> Option B
<input type="checkbox" name="user-prefs"> Option C
<script>
// Target image by ID
var imgElement = document.getElementById("target-image");
// Target divs by tag
var divList = document.getElementsByTagName("div");
for(var index = 0; index < divList.length; index++) {
divList[index].style.color = "green";
divList[index].innerHTML = "Updated Text Content";
}
// Target checkboxes by name
var checkInputs = document.getElementsByName("user-prefs");
for(let i = 0; i < checkInputs.length; i++) {
checkInputs[i].checked = true;
}
// Target items by class
var clsItems = document.getElementsByClassName("content-box");
for(let j = 0; j < clsItems.length; j++) {
// Logic to process class-based items
}
</script>
</body>
</html>
Binding Events Programmatically
Event listeners enable scripts to respond to user interactions. While inline attributes can trigger functions directly in HTML, assigning handlers via JavaScript property assignment is preferred for separating logic from markup.
Code Demonstration
This snippet illustrates both inline attachment and script-based property binding.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Event Listener Demo</title>
</head>
<body>
<input type="button" value="Inline Click" onclick="runHandler()">
<input type="button" value="JS Bound" id="btn-action">
<script>
function runHandler(){
alert("Inline event triggered");
}
var actionBtn = document.getElementById("btn-action");
actionBtn.onclick = function (){
alert("Script-bound event executed");
};
</script>
</body>
</html>
Managing Form Submissions
Forms are standard components for data input. The submit event fires when a form attempts to send data. Intercepting this event allows for custom validation or preventing default browser navigation.
Form Handler Example
The example below attaches a listener to prevent actual page reload during the demonstration.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Form Event Demo</title>
</head>
<body>
<form id="login-form" action="#">
<input type="text" name="username" placeholder="Username" />
<input type="submit" value="Submit">
</form>
<script>
document.getElementById("login-form").onsubmit = function(event){
alert("Submission intercepted!");
return false;
};
</script>
</body>
</html>