The Document Object Model (DOM) is a programming interface for HTML and XML documents. It represents the page so that programs can change the document structure, style, and content.
<html>
<head>
<title>Page Title</title>
</head>
<body>
<h1>Main Heading</h1>
<p>Paragraph text</p>
</body>
</html>
Try out DOM manipulation in real-time! Click the buttons below to see how different DOM methods work.
This is a demo paragraph.
Before you can manipulate DOM elements, you need to select them. JavaScript provides several methods to select elements from the DOM.
Selects a single element by its unique ID attribute. Fast and efficient for targeting specific elements.
document.getElementById('myElement')
Selects the first element matching a CSS selector. Very flexible and powerful.
document.querySelector('.myClass')
Selects all elements matching a CSS selector. Returns a NodeList collection.
document.querySelectorAll('div.item')
Selects elements by class name. Returns a live HTMLCollection that updates automatically.
document.getElementsByClassName('myClass')
This is a description paragraph.
Another description paragraph.
Once you've selected elements, you can manipulate them by changing their content, attributes, and structure.
createElement() - Create new elementcreateTextNode() - Create text nodeappendChild() - Add child elementinsertBefore() - Insert before elementremoveChild() - Remove child elementreplaceChild() - Replace elementremove() - Remove element (modern)cloneNode() - Clone elementOriginal content
You can modify the content and attributes of DOM elements to create dynamic web pages.
innerHTML - Get/set HTML contenttextContent - Get/set text contentinnerText - Get/set visible textgetAttribute() - Get attribute valuesetAttribute() - Set attribute valueremoveAttribute() - Remove attributeOriginal paragraph content.
Events are actions that happen in the browser, such as clicking a button, moving the mouse, or pressing a key. JavaScript can respond to these events.
There are many different types of events that can occur in a web browser. Here are the most common categories:
Handle user interactions with mouse clicks, movements, and hover states.
click, dblclick, mousedown, mouseup, mouseover, mouseout
Respond to keyboard input and key combinations for interactive experiences.
keydown, keyup, keypress
Handle form submissions, input changes, and focus states for user input validation.
submit, change, input, focus, blur
Monitor browser window changes like loading, resizing, and scrolling.
load, resize, scroll, beforeunload
Experience different event types in real-time! Interact with the elements below and watch the events fire.
There are several ways to bind events to elements in JavaScript. The most modern and flexible approach is using event listeners.
<button onclick="myFunction()">
Click me
</button>
element.onclick = function() {
// Handle click
};
element.addEventListener(
'click',
function() {
// Handle click
}
);
Event delegation is a technique where you attach a single event listener to a parent element to handle events for multiple child elements, including those added dynamically.