0% found this document useful (0 votes)
3 views4 pages

Dom Complete Guide Advanced

This document provides a comprehensive guide on JavaScript DOM manipulation and event handling, covering topics such as selecting elements, changing content and styles, handling various events, and managing multiple elements with querySelectorAll. It includes practical examples for each concept, including creating, adding, and removing elements, as well as toggling classes and attributes. Key notes emphasize the importance of using forEach for multiple elements and preventing default behaviors for form submissions.

Uploaded by

Design Team
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

Dom Complete Guide Advanced

This document provides a comprehensive guide on JavaScript DOM manipulation and event handling, covering topics such as selecting elements, changing content and styles, handling various events, and managing multiple elements with querySelectorAll. It includes practical examples for each concept, including creating, adding, and removing elements, as well as toggling classes and attributes. Key notes emphasize the importance of using forEach for multiple elements and preventing default behaviors for form submissions.

Uploaded by

Design Team
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

JavaScript DOM Manipulation & Event Handling (Complete Guide with Styles and

Multiple Elements)

This document explains DOM manipulation, event handling, styling, and handling multiple elements using
querySelectorAll in simple English with examples.

1. Selecting Elements

// Select by ID
let el = [Link]("title");

// Select first element by class


let el2 = [Link](".class");

// Select all elements by class or tag


let elements = [Link](".box");

2. Changing Content

[Link] = "Hello";
[Link] = "<b>Hello</b>";

3. Changing Style

[Link] = "red";
[Link] = "yellow";

4. Event Handling

Click Event

let btn = [Link]("btn");


[Link]("click", function() {

1
alert("Clicked!");
});

Input Event

let input = [Link]("input");


[Link]("input", function() {
[Link]([Link]);
});

Submit Event

let form = [Link]("form");


[Link]("submit", function(e) {
[Link]();
[Link]("Form Submitted");
});

Mouseover Event

[Link]("mouseover", function() {
[Link] = "yellow";
});

Keydown Event

[Link]("keydown", function(e) {
[Link]([Link]);
});

5. Create and Add Elements

let p = [Link]("p");
[Link] = "New Paragraph";
[Link](p);

2
6. Remove Elements

[Link]();
// OR
[Link](child);

7. Add / Remove / Toggle Class

[Link]("active");
[Link]("inactive");
[Link]("highlight");

8. Handling Multiple Elements with querySelectorAll

Change style of multiple elements

let boxes = [Link](".box");


[Link](function(box) {
[Link] = "yellow";
});

Add / Remove class on multiple elements

[Link](function(box) {
[Link]("active");
[Link]("inactive");
});

Add click event to multiple elements

[Link](function(box) {
[Link]("click", function() {
[Link] = "green";
});
});

Toggle class on multiple elements

3
[Link]("toggle").addEventListener("click", function() {
[Link](function(box) {
[Link]("highlight");
});
});

9. Attributes

[Link]("id", "newId");
[Link]("id");

10. Mini Example (Todo App)

<input id="task">
<button id="add">Add</button>
<ul id="list"></ul>

let btn = [Link]("add");


[Link]("click", function() {
let val = [Link]("task").value;
let li = [Link]("li");
[Link] = val;
[Link]("list").appendChild(li);
});

Key Notes

1. querySelectorAll returns multiple elements → use forEach loop


2. Use style for inline changes or classList for CSS class manipulation
3. Events can be added to multiple elements inside a loop
4. Always prevent default behavior for form submit if needed
5. DOM manipulation = selecting → changing → adding/removing → events

You might also like