Concept Notes: JavaScript – DOM & Events

🔹 1. What is DOM?

DOM (Document Object Model) is a programming interface that allows JavaScript to access and modify HTML and CSS content dynamically.

Think of it as a tree-like structure where every tag becomes a node.

Example:

<html>
  <body>
    <h1>Hello</h1>
    <button>Click Me</button>
  </body>
</html>

The browser converts this into a DOM tree, and JS can access each node like:

document.querySelector("h1").innerText = "Hi!";

🔹 2. DOM Methods

MethodPurpose
getElementById()Select by ID
getElementsByClassName()Select elements by class
querySelector()Select first match of any CSS selector
createElement(), appendChild()Add new elements dynamically

Example:

document.getElementById("title").innerText = "Updated!";

🔹 3. Events in JavaScript

Events are actions that happen in the browser — like clicks, mouse movement, form submission, etc.

🔹 4. Common Events

Event TypeTrigger Example
onclickWhen user clicks an element
onmouseoverHover over an element
onkeyupKeyboard key is released
onsubmitForm is submitted

Example:

<button onclick="alert('Button clicked!')">Click Me</button>

🔹 5. Event Listeners (Modern Way)

document.querySelector("button").addEventListener("click", function() {
  alert("Clicked using listener!");
});

🔹 6. DOM vs HTML

  • HTML is static content.
  • DOM is the live, interactive version JS can manipulate.
  • Changes in DOM using JS don’t change original HTML file but affect user experience.

🔹 7. Real-World Example

Form validation, interactive quiz, dynamic popups — all powered by DOM + Events.

🧠 10 MCQs – JavaScript DOM & Events

1️⃣ What does DOM stand for?
A) Data Object Mode
B) Document Object Model
C) Digital Order Map
D) Document Orientation Management

2️⃣ Which method is used to select an element by ID?
A) getId()
B) queryId()
C) getElementById()
D) selectById()

3️⃣ What is the output of:

document.querySelector("h1").innerText = "Hi";

A) Changes all h1
B) Changes first h1
C) Adds a new h1
D) Deletes h1

4️⃣ Which of the following is a valid event?
A) onrun
B) onclick
C) onprint
D) ondata

5️⃣ What is used to add an event listener?
A) onClick()
B) attachEvent()
C) addEventListener()
D) eventBind()

6️⃣ What is the DOM structure like?
A) Grid
B) Stack
C) Tree
D) Linked List

7️⃣ The method createElement() is used to:
A) Remove elements
B) Edit text
C) Create new elements
D) Delete CSS

8️⃣ DOM allows JS to:
A) Change server settings
B) Edit HTML/CSS dynamically
C) Compress files
D) Store in local DB

9️⃣ Which method selects an element using a CSS selector?
A) getCSS()
B) querySelector()
C) selectStyle()
D) cssSelect()

🔟 appendChild() is used to:
A) Delete an element
B) Add new child element
C) Style an element
D) Clone element


✅ Answer Key

Q.NoAnswer
1B
2C
3B
4B
5C
6C
7C
8B
9B
10B

📖 Explanations

  • Q1: DOM = Document Object Model.
  • Q2: getElementById() selects element by ID.
  • Q3: querySelector changes the first matching element.
  • Q4: onclick is a valid JS event.
  • Q5: addEventListener() is the modern method.
  • Q6: DOM is tree-structured.
  • Q7: createElement() makes a new node.
  • Q8: DOM lets JS edit page dynamically.
  • Q9: querySelector() selects via CSS.
  • Q10: appendChild() adds a node.

📥 Download Full Notes + MCQs

📲 Telegram – @learnnewthingsoffcial
PDF with examples + browser preview code.

💬 Comment Challenge

💬 Write a one-line JS code that changes an image’s src using DOM.

Loading

Leave a comment

Your email address will not be published. Required fields are marked *