DOM and Browser APIs
Interacting with web pages (Python has no equivalent)
Introduction
In this lesson, you'll learn about dom and browser apis in JavaScript. Coming from Python, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.
In Python, you're familiar with interacting with web pages (python has no equivalent).
JavaScript has its own approach to interacting with web pages (python has no equivalent), which we'll explore step by step.
The JavaScript Way
Let's see how JavaScript handles this concept. Here's a typical example:
// Select elements
const heading = document.querySelector("h1");
const items = document.querySelectorAll(".item");
// Read / modify content
heading.textContent = "Hello, World!";
heading.innerHTML = "<em>Hello</em>";
// Style
heading.style.color = "blue";
heading.classList.add("active");
heading.classList.toggle("hidden");
// Create & append
const btn = document.createElement("button");
btn.textContent = "Click me";
document.body.appendChild(btn);
// Events
btn.addEventListener("click", (e) => {
console.log("Clicked!", e.target);
});
// Fetch data and update UI
const res = await fetch("/api/data");
const data = await res.json();
heading.textContent = data.title;Comparing to Python
Here's how you might have written similar code in Python:
# Python runs on the SERVER — it has no DOM.
# Closest equivalents:
# 1. BeautifulSoup – parse HTML on the server
from bs4 import BeautifulSoup
soup = BeautifulSoup("<h1>Hello</h1>", "html.parser")
h1 = soup.find("h1")
print(h1.text) # "Hello"
# 2. Selenium – control a browser from Python
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://example.com")
el = driver.find_element("id", "btn")
el.click()
# 3. Flask/Django – generate HTML dynamically
# There is no direct equivalent to live DOM manipulation.You may be used to different syntax or behavior.
DOM manipulation is JavaScript's unique superpower — Python cannot do this in browsers
You may be used to different syntax or behavior.
querySelector/querySelectorAll select elements by CSS selector
You may be used to different syntax or behavior.
Events (click, keydown, submit) are central to front-end JS
You may be used to different syntax or behavior.
Fetch + DOM update is the core web development loop
Step-by-Step Breakdown
1. Selecting Elements
querySelector returns the first match; querySelectorAll returns a NodeList of all matches.
# No equivalent — Python runs on the serverdocument.querySelector("#id") // by id
document.querySelector(".class") // by class
document.querySelectorAll("li") // all <li> elements2. Modifying the DOM
textContent is safe (escapes HTML); innerHTML renders HTML (use carefully to avoid XSS).
# Python renders HTML before it reaches the browserel.textContent = "Safe text";
el.innerHTML = "<b>Bold</b>"; // ⚠️ XSS risk with user input3. Event Listeners
addEventListener attaches a function that fires when the event occurs.
# Python: handle HTTP requests (not live user events)btn.addEventListener("click", (e) => {
console.log(e.target);
});
// Remove it:
btn.removeEventListener("click", handler);Common Mistakes
When coming from Python, developers often make these mistakes:
- DOM manipulation is JavaScript's unique superpower — Python cannot do this in browsers
- querySelector/querySelectorAll select elements by CSS selector
- Events (click, keydown, submit) are central to front-end JS
Key Takeaways
- DOM is JavaScript-only — Python manipulates HTML on the server
- querySelector/querySelectorAll for element selection
- addEventListener for user interaction