Module-02
Document Object Model
1. Introduction to DOM
• DOM (Document Object Model) represents the structure of an HTML
document as a tree of objects.
• JavaScript can access and manipulate these elements dynamically.
2. Selecting Elements in DOM
• Using JavaScript:
o [Link]("id") – Selects an element by ID.
o [Link]("class") – Selects elements by
class.
o [Link]("tag") – Selects elements by tag
name.
o [Link]("selector") – Selects the first matching
element.
o [Link]("selector") – Selects all matching
elements.
• Using jQuery:
o $("selector") – Selects elements using CSS-like syntax.
3. Modifying Elements
• JavaScript:
o [Link] = "New Content"; – Changes content inside an
element.
o [Link] = "New Text"; – Updates only text content.
o [Link]("attribute", "value"); – Modifies attributes.
• jQuery:
o $("selector").html("New Content");
o $("selector").text("New Text");
o $("selector").attr("attribute", "value");
4. Adding & Removing Elements
• JavaScript:
o [Link]("tag") – Creates a new element.
o [Link](child) – Adds a child element.
o [Link](child) – Removes an element.
• jQuery:
o $("parent").append("<tag>New Content</tag>");
o $("parent").prepend("<tag>New Content</tag>");
o $("selector").remove();
BIS601 | FULLSTACK DEVELOPMENT | SEARCH CREATORS.
5. Handling Events
• JavaScript:
o [Link]("event", function);
• jQuery:
o $("selector").on("event", function() {});
• Example:
[Link]("btn").addEventListener("click", function() {
alert("Button Clicked!");
});
$("#btn").on("click", function() {
alert("Button Clicked!");
});
6. Changing CSS Styles
• JavaScript:
o [Link] = "value";
• jQuery:
o $("selector").css("property", "value");
• Example:
Search Creators... Page 4
BIS601 | FULLSTACK DEVELOPMENT | SEARCH CREATORS.
[Link]("box").[Link] = "blue";
$("#box").css("background-color", "blue");
7. Animations & Effects in jQuery
• $("selector").fadeIn();
• $("selector").fadeOut();
• $("selector").slideUp();
• $("selector").slideDown();
• $("selector").animate({property: "value"}, duration);
8. AJAX and Fetching Data
• JavaScript Fetch API:
fetch("url")
.then(response => [Link]())
.then(data => [Link](data));
• jQuery AJAX:
$.ajax({
url: "url",
method: "GET",
success: function(data) {
[Link](data);
Search Creators... Page 5
BIS601 | FULLSTACK DEVELOPMENT | SEARCH CREATORS.
});
9. Event Delegation
• Used for handling events dynamically on elements that may not exist at the
start.
• JavaScript:
[Link]("parent").addEventListener("click", function(event) {
if ([Link](".child")) {
[Link]("Child clicked!");
});
• jQuery:
$("#parent").on("click", ".child", function() {
[Link]("Child clicked!");
});
10. Form Handling & Validation
• Accessing form values:
let inputValue = [Link]("input").value;
let inputValue = $("#input").val();
Search Creators... Page 6
BIS601 | FULLSTACK DEVELOPMENT | SEARCH CREATORS.
• Validating input fields:
if ([Link]() === "") {
alert("Field cannot be empty");
1. DOM Node
• A node is any object in the DOM tree, such as:
o Element nodes (<div>, <p>, <button>, etc.)
o Attribute nodes (class, id, style)
o Text nodes (content inside an element)
o Comment nodes (<!-- This is a comment -->)
2. Accessing Nodes
Using JavaScript
• [Link]("id") → Selects an element by ID.
• [Link]("class") → Selects elements by class
name.
• [Link]("tag") → Selects elements by tag name.
• [Link]("selector") → Selects the first matching element.
• [Link]("selector") → Selects all matching elements.
Search Creators... Page 7
BIS601 | FULLSTACK DEVELOPMENT | SEARCH CREATORS.
Using jQuery
• $("selector") → Selects elements using CSS-like syntax.
Example:
let node = [Link]("demo"); // JavaScript
let node = $("#demo"); // jQuery
3. Creating & Adding Nodes
Using JavaScript
• [Link]("tag") → Creates a new element.
• [Link](child) → Appends a child node to a parent.
• [Link](newNode, existingNode) → Inserts before another node.
Example:
let newDiv = [Link]("div");
[Link] = "New Element";
[Link](newDiv);
Using jQuery
• $("parent").append("<div>New Element</div>");
• $("parent").prepend("<div>New at Start</div>");
Search Creators... Page 8
BIS601 | FULLSTACK DEVELOPMENT | SEARCH CREATORS.
Example:
$("body").append("<div>New Element</div>");
4. Removing & Replacing Nodes
Using JavaScript
• [Link](child) → Removes a node.
• [Link](newNode, oldNode) → Replaces a node.
Example:
let toRemove = [Link]("remove");
[Link](toRemove);
Using jQuery
• $("selector").remove(); → Removes an element.
• $("selector").replaceWith("<new element>"); → Replaces an element.
Example:
$("#remove").remove();
$("#old").replaceWith("<p>New Paragraph</p>");
Search Creators... Page 9
BIS601 | FULLSTACK DEVELOPMENT | SEARCH CREATORS.
5. Cloning & Copying Nodes
Using JavaScript
• [Link](true) → Clones an element with all children (true) or
only itself (false).
Example:
let original = [Link]("original");
let clone = [Link](true);
[Link](clone);
Using jQuery
• $("selector").clone();
Example:
let clonedElement = $("#original").clone();
$("body").append(clonedElement);
Search Creators... Page 10
BIS601 | FULLSTACK DEVELOPMENT | SEARCH CREATORS.
6. Navigating Nodes (Parent, Child, Sibling)
Using JavaScript
• [Link] → Gets the parent node.
• [Link] → Gets all child elements.
• [Link] / [Link] → Gets the first/last child.
• [Link] / [Link] → Gets the next/previous
node.
Example:
let parent = [Link]("child").parentNode;
let firstChild = [Link]("parent").firstChild;
Using jQuery
• $("selector").parent();
• $("selector").children();
• $("selector").first();
• $("selector").next();
Example:
let parent = $("#child").parent();
let firstChild = $("#parent").children().first();
Search Creators... Page 11
BIS601 | FULLSTACK DEVELOPMENT | SEARCH CREATORS.
7. Changing Node Content
Using JavaScript
• [Link] = "New Content"; → Changes inner content.
• [Link] = "New Text"; → Changes text only.
Using jQuery
• $("selector").html("New Content");
• $("selector").text("New Text");
Example:
[Link]("demo").innerHTML = "Updated Content";
$("#demo").html("Updated Content");
8. Changing Node Attributes
Using JavaScript
• [Link]("attribute", "value");
• [Link]("attribute");
• [Link]("attribute");
Using jQuery
• $("selector").attr("attribute", "value");
• $("selector").removeAttr("attribute");
Search Creators... Page 12
BIS601 | FULLSTACK DEVELOPMENT | SEARCH CREATORS.
Example:
[Link]("demo").setAttribute("class", "new-class");
$("#demo").attr("class", "new-class");
1. Updating Element Content
• innerHTML (JS) / .html() (jQuery) → Changes the inner HTML,
including formatting.
• textContent (JS) / .text() (jQuery) → Updates text content only (ignores
HTML tags).
• Example:
[Link]("demo").innerHTML = "<b>Bold Text</b>"; //
JavaScript
$("#demo").html("<b>Bold Text</b>"); // jQuery
2. Updating Element Attributes
• setAttribute("attr", "value") (JS) / .attr("attr", "value") (jQuery) →
Modifies an attribute.
• getAttribute("attr") (JS) / .attr("attr") (jQuery) → Retrieves an attribute
value.
• removeAttribute("attr") (JS) / .removeAttr("attr") (jQuery) → Deletes
an attribute.
Search Creators... Page 13
BIS601 | FULLSTACK DEVELOPMENT | SEARCH CREATORS.
• Example:
[Link]("myImage").setAttribute("src", "[Link]"); //
JavaScript
$("#myImage").attr("src", "[Link]"); // jQuery
3. Handling Events
• JavaScript: addEventListener("event", function) → Binds an event to an
element.
• jQuery: .on("event", function) → Binds an event (can handle multiple
events).
• Common events: click, mouseover, keydown, change.
• Example:
[Link]("btn").addEventListener("click", function() {
alert("Clicked!");
});
$("#btn").on("click", function() {
alert("Clicked!");
});
Search Creators... Page 14
BIS601 | FULLSTACK DEVELOPMENT | SEARCH CREATORS.
4. Removing Event Listeners
• JavaScript: removeEventListener("event", function)
• jQuery: .off("event")
• Example:
[Link]("btn").removeEventListener("click", myFunction);
$("#btn").off("click");
Different Types of Events in JavaScript & jQuery
Events in JavaScript and jQuery allow developers to handle user interactions
effectively. Below are the key types of events:
1. Mouse Events
These events respond to user actions with the mouse.
Search Creators... Page 15
BIS601 | FULLSTACK DEVELOPMENT | SEARCH CREATORS.
[Link]("btn").addEventListener("click", function() {
alert("Button clicked!");
});
// jQuery
$("#btn").on("click", function() {
alert("Button clicked!");
});
2. Keyboard Events
These events respond to keyboard interactions.
[Link]("keydown", function(event) {
[Link]("Key pressed: " + [Link]);
});
Search Creators... Page 16
BIS601 | FULLSTACK DEVELOPMENT | SEARCH CREATORS.
// jQuery
$(document).on("keydown", function(event) {
[Link]("Key pressed: " + [Link]);
});
3. Form Events
These events are related to form elements.
[Link]("myForm").addEventListener("submit",
function(event) {
[Link](); // Prevents form from submitting
alert("Form submitted!");
});
// jQuery
$("#myForm").on("submit", function(event) {
[Link]();
Search Creators... Page 17
BIS601 | FULLSTACK DEVELOPMENT | SEARCH CREATORS.
alert("Form submitted!");
});
4. Window Events
These events are triggered by the browser window.
[Link]("resize", function() {
[Link]("Window resized!");
});
// jQuery
$(window).on("resize", function() {
[Link]("Window resized!");
});
Search Creators... Page 18
BIS601 | FULLSTACK DEVELOPMENT | SEARCH CREATORS.
5. Clipboard Events
These events handle copying and pasting actions.
[Link]("copy", function() {
alert("Content copied!");
});
// jQuery
$(document).on("copy", function() {
alert("Content copied!");
});
6. Drag & Drop Events
These events allow users to drag and drop elements.
Search Creators... Page 19
BIS601 | FULLSTACK DEVELOPMENT | SEARCH CREATORS.
[Link]("dragElement").addEventListener("dragstart",
function() {
[Link]("Dragging started!");
});
// jQuery
$("#dragElement").on("dragstart", function() {
[Link]("Dragging started!");
});
7. Media Events
These events are used for media elements like <audio> and <video>.
[Link]("myVideo").addEventListener("play", function() {
Search Creators... Page 20
BIS601 | FULLSTACK DEVELOPMENT | SEARCH CREATORS.
[Link]("Video started playing!");
});
// jQuery
$("#myVideo").on("play", function() {
[Link]("Video started playing!");
});
8. Touch Events (Mobile Devices)
These events handle touch interactions on mobile devices.
[Link]("touchstart", function() {
[Link]("Screen touched!");
});
// jQuery
Search Creators... Page 21
BIS601 | FULLSTACK DEVELOPMENT | SEARCH CREATORS.
$(document).on("touchstart", function() {
[Link]("Screen touched!");
});
1. Using JavaScript (addEventListener)
• The addEventListener(event, function) method is used to bind an event to an
element.
• Syntax:
[Link]("event", function);
• Example:
[Link]("btn").addEventListener("click", function() {
alert("Button clicked!");
});
• Supports multiple event bindings for the same element.
• Does not override existing event listeners.
2. Using JavaScript (onclick or Inline Event Handler)
• Assigning an event handler directly to the onclick property.
Search Creators... Page 22
BIS601 | FULLSTACK DEVELOPMENT | SEARCH CREATORS.
• Syntax:
[Link] = function;
• Example:
[Link]("btn").onclick = function() {
alert("Button clicked!");
};
• ⚠ Limitations:
o Can only bind one event at a time (overwrites previous event).
o Not suitable for dynamic elements.
3. Using jQuery (.on())
• The .on(event, selector, function) method is used to bind events
dynamically.
• Syntax:
$(selector).on("event", function);
• Example:
$("#btn").on("click", function() {
alert("Button clicked!");
});
Search Creators... Page 23
BIS601 | FULLSTACK DEVELOPMENT | SEARCH CREATORS.
• Works on dynamically added elements.
• Can bind multiple events at once.
4. Using jQuery (.click(), .hover(), etc.)
• Shortcut methods for specific events.
• Example:
$("#btn").click(function() {
alert("Button clicked!");
});
• Simpler syntax but does not work for dynamically created elements.
5. Binding Multiple Events to the Same Element
• JavaScript (addEventListener):
let btn = [Link]("btn");
[Link]("mouseover", function() { [Link]("Mouse over!"); });
[Link]("click", function() { [Link]("Button clicked!"); });
• jQuery (.on()):
$("#btn").on("mouseover click", function() {
[Link]("Mouse over or clicked!");
Search Creators... Page 24
BIS601 | FULLSTACK DEVELOPMENT | SEARCH CREATORS.
});
6. Binding Events to Multiple Elements
• JavaScript (querySelectorAll):
[Link](".buttons").forEach(button => {
[Link]("click", function() {
alert("One of the buttons clicked!");
});
});
• jQuery (.each()):
$(".buttons").each(function() {
$(this).on("click", function() {
alert("One of the buttons clicked!");
});
});
7. Binding Events to Dynamically Added Elements
• JavaScript (Event Delegation using [Link]):
[Link]("click", function(event) {
Search Creators... Page 25
BIS601 | FULLSTACK DEVELOPMENT | SEARCH CREATORS.
if ([Link]("dynamic-btn")) {
alert("Dynamically added button clicked!");
});
• jQuery (.on()):
$(document).on("click", ".dynamic-btn", function() {
alert("Dynamically added button clicked!");
});
• Works for elements added later via JavaScript/jQuery.
8. Removing an Event Binding
• JavaScript (removeEventListener):
function myFunction() {
alert("Clicked!");
[Link]("btn").addEventListener("click", myFunction);
[Link]("btn").removeEventListener("click", myFunction);
• jQuery (.off()):
$("#btn").on("click", function() { alert("Clicked!"); });
Search Creators... Page 26
BIS601 | FULLSTACK DEVELOPMENT | SEARCH CREATORS.
$("#btn").off("click"); // Removes click event
Event Delegation in JavaScript & jQuery
Event Delegation
Event delegation is a technique in JavaScript where a single event listener is added
to a parent element to handle events on multiple child elements, including
dynamically added elements.
Efficient – Reduces memory usage by attaching fewer event listeners.
Handles Dynamic Elements – Works even when new elements are added
dynamically.
Better Performance – Instead of attaching events to every element, it
delegates handling to a common parent.
How Event Delegation Works?
1. Add an event listener to a common parent element.
Search Creators... Page 27
BIS601 | FULLSTACK DEVELOPMENT | SEARCH CREATORS.
2. Use [Link] to check if the event was triggered by the intended child
element.
3. Execute the event handler only if the correct child element is clicked.
1. Event Delegation Using JavaScript
Example: Handling Clicks on a List of Items
[Link]("list").addEventListener("click", function(event) {
if ([Link] === "LI") {
alert("Clicked on: " + [Link]);
});
The event listener is attached to the #list parent, but it works for all <li>
elements inside it.
Works even for new <li> items added later.
2. Event Delegation Using jQuery
Example: Handling Clicks on Buttons inside a Parent Div
Search Creators... Page 28
BIS601 | FULLSTACK DEVELOPMENT | SEARCH CREATORS.
$(document).on("click", ".child-btn", function() {
alert("Button clicked: " + $(this).text());
});
The event is attached to document, but it listens for .child-btn clicks.
Works for dynamically added .child-btn elements.
4. When to Use Event Delegation?
When dealing with lists, tables, or elements created dynamically (e.g., <ul>,
<table>, <div>)
When handling repetitive UI components (e.g., buttons inside cards, form
fields inside a container)
When optimizing performance for high-interaction web pages
5. When Not to Use Event Delegation?
Search Creators... Page 29
BIS601 | FULLSTACK DEVELOPMENT | SEARCH CREATORS.
If the child elements have different event handlers that must be unique
If capturing event behavior is needed (i.e., focus, blur, mouseenter)
If immediate response is required (event delegation may have a slight delay in
large DOM structures)
6. Real-World Example
Dynamically Adding and Handling Click Events
[Link]("addItem").addEventListener("click", function() {
let newItem = [Link]("li");
[Link] = "New Item";
[Link]("list").appendChild(newItem);
});
// Event Delegation for Handling Clicks
[Link]("list").addEventListener("click", function(event) {
if ([Link] === "LI") {
alert("You clicked on: " + [Link]);
});
Search Creators... Page 30
BIS601 | FULLSTACK DEVELOPMENT | SEARCH CREATORS.
Clicking on newly added <li> items still triggers the event!
Event Listeners in JavaScript & jQuery
What is an Event Listener?
An event listener is a function that waits for a specific event (like a click, hover,
or keypress) to happen on an element and executes a function in response.
Detects user interactions (clicks, keyboard input, scrolling, etc.)
Enhances interactivity in web applications
Can be applied to multiple elements without modifying HTML
1. Adding Event Listeners in JavaScript
1.1 Using addEventListener()
The addEventListener() method attaches an event to an element without
overwriting existing events.
Syntax:
[Link]("event", function, useCapture);
• "event" → Type of event (e.g., "click", "mouseover", "keydown")
• function → The function to execute when the event occurs
• useCapture (optional) → true for event capturing, false for bubbling
Example: Button Click Listener
[Link]("btn").addEventListener("click", function() {
Search Creators... Page 31
BIS601 | FULLSTACK DEVELOPMENT | SEARCH CREATORS.
alert("Button Clicked!");
});
Supports multiple event listeners on the same element
More flexible than onclick
1.2 Using Inline Event Handlers (onclick, onmouseover)
You can directly assign an event to an element using properties like onclick.
Example:
[Link]("btn").onclick = function() {
alert("Button Clicked!");
};
⚠ Limitations:
• Overwrites any previously assigned event handlers
• Not ideal for handling multiple events
Search Creators... Page 32
BIS601 | FULLSTACK DEVELOPMENT | SEARCH CREATORS.
1.3 Using Anonymous vs Named Functions
Both anonymous and named functions can be used in event listeners.
Anonymous Function (Inline Function)
[Link]("btn").addEventListener("click", function() {
alert("Button Clicked!");
});
Named Function
function showAlert() {
alert("Button Clicked!");
[Link]("btn").addEventListener("click", showAlert);
Search Creators... Page 33
BIS601 | FULLSTACK DEVELOPMENT | SEARCH CREATORS.
Advantage of Named Function: Can be reused and removed easily
2. Removing Event Listeners
Use removeEventListener() to detach an event listener.
[Link]("btn").removeEventListener("click", showAlert);
⚠ The function must be named when removing it. Anonymous functions cannot
be removed.
3. Event Listeners in jQuery
3.1 Using .on() (Recommended)
The .on() method attaches an event listener dynamically.
$("#btn").on("click", function() {
alert("Button Clicked!");
});
Works even for dynamically added elements
Can bind multiple events
3.2 Using .click(), .hover(), .keypress() (Shortcut Methods)
$("#btn").click(function() {
Search Creators... Page 34
BIS601 | FULLSTACK DEVELOPMENT | SEARCH CREATORS.
alert("Button Clicked!");
});
⚠ These methods do not work on dynamically added elements. Use .on()
instead.
4. Event Propagation (Bubbling & Capturing)
Events travel through the DOM in two phases:
1️⃣ Capturing Phase (Top to Bottom)
2️⃣ Bubbling Phase (Bottom to Top)
Example: Bubbling Effect
[Link]("parent").addEventListener("click", function() {
alert("Parent Clicked!");
});
[Link]("child").addEventListener("click", function(event) {
alert("Child Clicked!");
});
Clicking on #child will trigger both listeners (child → parent).
Stopping Event Bubbling
Use [Link]() to prevent bubbling.
Search Creators... Page 35
BIS601 | FULLSTACK DEVELOPMENT | SEARCH CREATORS.
[Link]("child").addEventListener("click", function(event) {
[Link]();
alert("Child Clicked, but parent won't trigger!");
});
5. Binding Multiple Events to an Element
Attach multiple event listeners using addEventListener() or .on().
JavaScript Example
let btn = [Link]("btn");
[Link]("mouseover", function() { [Link]("Mouse Over!"); });
[Link]("click", function() { [Link]("Button Clicked!"); });
jQuery Example
$("#btn").on("mouseover click", function() {
[Link]("Mouse Over or Clicked!");
});
6. Event Delegation
Instead of attaching listeners to each element, attach it to a parent and use
[Link] to handle child elements dynamically.
Search Creators... Page 36
BIS601 | FULLSTACK DEVELOPMENT | SEARCH CREATORS.
JavaScript Example
[Link]("parent").addEventListener("click", function(event) {
if ([Link] === "BUTTON") {
alert("Button inside parent clicked!");
});
jQuery Example
$(document).on("click", ".child-btn", function() {
alert("Dynamically added button clicked!");
});
Best for handling dynamically generated elements.
7. Common Event Listener Types
Search Creators... Page 37
BIS601 | FULLSTACK DEVELOPMENT | SEARCH CREATORS.
Search Creators... Page 38