Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 

Repository files navigation

DOM and Event Listeners: Techniques & Usage

Introduction

In web development, the DOM (Document Object Model) represents the structure of an HTML document as a tree of objects. JavaScript allows us to interact with and manipulate this structure.

Event Listeners are essential for making web pages interactive. They allow us to capture user actions (like clicks, key presses, etc.) and trigger responses.

This guide includes techniques for:

  • Selecting, creating, modifying, and deleting DOM elements.
  • Dynamically changing styles, attributes, and classes.
  • Inserting and manipulating HTML content.
  • Traversing the DOM tree.

1. Selecting Elements

Selecting by ID

let element = document.getElementById("myElement");

Selecting by Class Name

let elements = document.getElementsByClassName("myClass");

Selecting by CSS Selector

let element = document.querySelector(".myClass");

Selecting All Matching Elements

let elements = document.querySelectorAll(".myClass");

2. Manipulating Styles

Changing Styles Dynamically

let element = document.getElementById("myElement");
element.style.backgroundColor = "blue";
element.style.width = "200px";

3. Manipulating Attributes

Getting an Attribute

let srcValue = document.getElementById("myImage").getAttribute("src");

Setting an Attribute

document.getElementById("myImage").setAttribute("alt", "New Image");

Removing an Attribute

document.getElementById("myImage").removeAttribute("src");

4. Manipulating Classes

Adding a Class

document.getElementById("myElement").classList.add("newClass");

Removing a Class

document.getElementById("myElement").classList.remove("oldClass");

Toggling a Class

document.getElementById("myElement").classList.toggle("highlight");

5. Event Listeners

Adding an Event Listener

document.getElementById("myButton").addEventListener("click", function() {
  alert("Button clicked!");
});

Handling Events with the Event Object

document.getElementById("myButton").addEventListener("click", function(event) {
  console.log("Event type:", event.type);
  console.log("Clicked element:", event.target);
});

6. DOM Traversing

Parent Traversal

let parentElement = document.getElementById("myElement").parentNode;

Child Traversal

let childElements = document.getElementById("parent").children;

Sibling Traversal

let nextSibling = document.getElementById("myElement").nextElementSibling;

7. Inserting and Modifying HTML

Using innerHTML

document.getElementById("myElement").innerHTML = "<p>New content</p>";

Using innerText

document.getElementById("myElement").innerText = "New Text";

Using insertAdjacentHTML

document.getElementById("parent").insertAdjacentHTML("beforeend", "<li>New Item</li>");

Positions:

  • beforebegin: Before the element itself.
  • afterbegin: Inside the element, before its first child.
  • beforeend: Inside the element, after its last child.
  • afterend: After the element itself.

8. Dynamic HTML Insertion

Dynamic HTML insertion allows adding new elements to the DOM without reloading the page.

Creating and Appending Elements

let newDiv = document.createElement("div");
newDiv.innerText = "This is a dynamically added div";
document.body.appendChild(newDiv);

Removing an Element

let elementToRemove = document.getElementById("myElement");
elementToRemove.remove();

Replacing an Element

let newElement = document.createElement("p");
newElement.innerText = "This is a new paragraph";
let oldElement = document.getElementById("myElement");
oldElement.replaceWith(newElement);

Inserting Before a Specific Element

let newItem = document.createElement("li");
newItem.innerText = "New Item";
let list = document.getElementById("parent");
list.insertBefore(newItem, list.firstChild);

9. Full Example: DOM Traversing, Event Listeners, and Dynamic HTML Insertion

<ul id="parent">
  <li id="item1">Item 1</li>
  <li id="item2">Item 2</li>
  <li id="item3">Item 3</li>
</ul>

<button id="toggleButton">Toggle Class</button>
<div id="myElement" class="box">This is a box.</div>

<script>
  // Select the second list item
  let item2 = document.getElementById("item2");

  // Navigate to the first item (parent-child traversal)
  let item1 = item2.previousElementSibling;
  console.log(item1.innerText);

  // Navigate to the next item (sibling traversal)
  let item3 = item2.nextElementSibling;
  console.log(item3.innerText);

  // Toggle the class on button click
  document.getElementById("toggleButton").addEventListener("click", function() {
    document.getElementById("myElement").classList.toggle("highlight");
  });
});
</script>

Conclusion

Mastering DOM manipulation and event listeners is essential for creating interactive web pages. With these techniques, you can dynamically modify elements, handle user interactions, and traverse the DOM efficiently. Happy coding! 🚀

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors