• Wrong document? Swap it for free
  • Written by students who passed
  • Immediately available after payment
  • Read online or as PDF
Sell
Where do you study
Your language
Document preview thumbnail
Preview 2 out of 6 pages
Other

Events and DOM Manipulation in JavaScript: A Beginner’s Guide with Examples

Document preview thumbnail
Preview 2 out of 6 pages

This document explains events and DOM manipulation in JavaScript, covering how to handle user interactions with event listeners and modify HTML elements dynamically. Learn with practical examples to build interactive web pages.

Content preview

Events and DOM Manipulation in JavaScript
JavaScript provides powerful tools to interact with and manipulate the Document
Object Model (DOM). Events allow developers to respond to user interactions
such as clicks, keypresses, and mouse movements.

1. DOM Basics
Accessing DOM Elements

 getElementById: Retrieves an element by its id.

const element = document.getElementById("myElement");
console.log(element);

 getElementsByClassName: Retrieves elements by class name.

const elements = document.getElementsByClassName("myClass");
console.log(elements);

 getElementsByTagName: Retrieves elements by tag name.

const paragraphs = document.getElementsByTagName("p");
console.log(paragraphs);

 querySelector: Retrieves the first element matching a CSS selector.

const firstDiv = document.querySelector("div");
console.log(firstDiv);

 querySelectorAll: Retrieves all elements matching a CSS selector.

const allDivs = document.querySelectorAll("div");
console.log(allDivs);

, 2. Manipulating DOM Elements
Changing Content

 innerHTML: Updates the HTML content.

element.innerHTML = "<strong>New Content</strong>";

 textContent: Updates only the text content.

element.textContent = "Plain text content";

Modifying Attributes

 setAttribute: Sets or modifies an attribute.

element.setAttribute("class", "newClass");

 getAttribute: Retrieves the value of an attribute.

console.log(element.getAttribute("id"));

 removeAttribute: Removes an attribute.

element.removeAttribute("class");

Styling Elements

 Inline CSS

element.style.color = "blue";
element.style.fontSize = "20px";

 Class Manipulation

element.classList.add("highlight");
element.classList.remove("highlight");
element.classList.toggle("active");

Document information

Uploaded on
January 27, 2025
Number of pages
6
Written in
2024/2025
Type
Other
Person
Unknown
$6.59

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Sold
0
Followers
0
Items
252
Last sold
-




Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions