Here's the complete README.md with all sections numbered and the updated explanation of window.document and globalThis:
This guide provides an overview of the most common actions you can perform on DOM elements using JavaScript.
- Introduction to Web APIs
- The
windowandglobalThis - Selecting Elements
- Changing HTML Content
- Changing CSS Styles
- Adding/Removing Classes
- Creating and Appending Elements
- Removing Elements
- Handling Events
Web APIs are interfaces provided by the browser that allow you to interact with various components of a web page. They expose functionalities like modifying the content of the page (DOM manipulation), interacting with the network, responding to user input, and much more.
- DOM (Document Object Model) API: Allows you to manipulate HTML and XML documents by accessing elements, attributes, and content.
- Fetch API: Provides an interface for fetching resources across the network.
- Local Storage API: Enables you to store key-value pairs in a web browser.
- Geolocation API: Gives access to the geographical location of a user’s device.
These APIs help make the web more interactive by providing an environment in which you can build dynamic web applications that respond to user actions.
In web development, the global window object represents the browser window and is the root of the DOM hierarchy. The document object, accessible as window.document, is a property of the window object and represents the entire webpage loaded in the browser.
The document object provides the interface for interacting with the DOM (Document Object Model) of the webpage, allowing you to access, manipulate, and modify elements, styles, and content dynamically.
Starting with modern JavaScript (ES2020), the globalThis object was introduced to access the global object in any JavaScript environment. In a browser, globalThis is equivalent to the window object.
- In the browser environment:
globalThis === window - In Node.js or other JavaScript environments:
globalThispoints to the global object of that environment.
This ensures consistency when writing JavaScript code that might run in different environments.
console.log(globalThis === window); // true in browsersUsing globalThis makes your code more portable, but when specifically targeting browser environments, using window is more common.
The document object is part of the window (or globalThis in the browser) and serves as the primary way to interact with the web page’s structure. It represents the entire HTML document loaded into the browser and provides numerous methods to access and manipulate elements in the DOM.
You can:
- Select elements using methods like
getElementByIdorquerySelector. - Create new elements dynamically with
document.createElement. - Listen for events like user clicks or form submissions using
addEventListener.
// Accessing the document using window or globalThis
console.log(window.document === globalThis.document); // true
// Manipulating the document
document.title = "New Page Title"; // Changes the page titleIn essence, window.document (or globalThis.document) is the gateway through which you can programmatically interact with the web page, making it dynamic and responsive.
To manipulate elements in the DOM, you first need to select them. You can do this using several methods:
let element = document.getElementById("myElement");let elements = document.getElementsByClassName("myClass");let element = document.querySelector("#myElement"); // Selects by ID
let elements = document.querySelectorAll(".myClass"); // Selects all by classYou can change the content of an element using innerHTML, textContent, or other properties.
let element = document.getElementById("myElement");
element.innerHTML = "<p>New content</p>";element.textContent = "New text content";You can modify the inline styles of an element using style.
element.style.backgroundColor = "lightblue";element.style.cssText = "color: red; font-size: 20px;";Manipulating classes is a common action to change the styling or behavior of elements.
element.classList.add("newClass");element.classList.remove("oldClass");element.classList.toggle("active");You can create new elements and append them to the DOM.
let newElement = document.createElement("div");
newElement.textContent = "I am a new element";
document.body.appendChild(newElement);let parentElement = document.getElementById("parent");
let referenceElement = document.getElementById("child");
parentElement.insertBefore(newElement, referenceElement);You can also remove elements from the DOM.
let elementToRemove = document.getElementById("myElement");
elementToRemove.remove();You can attach event listeners to handle user interactions.
element.addEventListener("click", function () {
alert("Element clicked!");
});let inputElement = document.getElementById("myInput");
inputElement.addEventListener("change", function () {
console.log("Input value changed to: ", inputElement.value);
});These are some of the main actions you can perform to manipulate the DOM with JavaScript. By using these methods, you can dynamically create, update, style, and remove elements to create interactive and dynamic web pages.