๐๐๐ฆ๐๐ข ๐๐ฅ๐๐ ๐๐ข๐จ๐ฅ๐ฆ๐๐ฆ ๐ฎ๐ฌ๐ฎ๐ฒ! ๐
Boost your Resume & LinkedIn Profile with valuable tech skills
Explore FREE Cisco learning resources! ๐
๐ Networking Fundamentals
๐ Cybersecurity
โ๏ธ Cloud & DevOps
๐ป IT Fundamentals
๐ฏ Perfect for Students, Freshers & Job Seekers!
๐๐ถ๐ป๐ธ๐:-
https://pdlink.in/3WPl0Qg
๐ SAVE & SHARE with friends interested in Tech Careers! ๐ฅ
Boost your Resume & LinkedIn Profile with valuable tech skills
Explore FREE Cisco learning resources! ๐
๐ Networking Fundamentals
๐ Cybersecurity
โ๏ธ Cloud & DevOps
๐ป IT Fundamentals
๐ฏ Perfect for Students, Freshers & Job Seekers!
๐๐ถ๐ป๐ธ๐:-
https://pdlink.in/3WPl0Qg
๐ SAVE & SHARE with friends interested in Tech Careers! ๐ฅ
โค2
๐ ๐ง๐ข๐ฃ ๐ฐ ๐๐ฅ๐๐ ๐๐๐ฉ๐ข๐ฃ๐ฆ ๐๐ข๐จ๐ฅ๐ฆ๐๐ฆ ๐ฎ๐ฌ๐ฎ๐ฒ! โ๏ธ
๐ Explore 4 FREE learning resources to build valuable DevOps skills!
โ Beginner-Friendly Resources
โ Learn at Your Own Pace
โ Build In-Demand Technical Skills
๐ฏ Perfect for Students, Freshers & Aspiring DevOps Engineers!
๐๐ถ๐ป๐ธ๐:-
https://pdlink.in/3WKDbGO
๐ SAVE & SHARE with friends interested in DevOps careers! ๐ฅ
๐ Explore 4 FREE learning resources to build valuable DevOps skills!
โ Beginner-Friendly Resources
โ Learn at Your Own Pace
โ Build In-Demand Technical Skills
๐ฏ Perfect for Students, Freshers & Aspiring DevOps Engineers!
๐๐ถ๐ป๐ธ๐:-
https://pdlink.in/3WKDbGO
๐ SAVE & SHARE with friends interested in DevOps careers! ๐ฅ
โค1
๐ง๐ข๐ฃ ๐ฑ ๐๐ฅ๐๐ ๐ฅ๐๐ฆ๐ข๐จ๐ฅ๐๐๐ฆ ๐ง๐ข ๐ ๐๐ฆ๐ง๐๐ฅ ๐ฆ๐ค๐! ๐
Discover 5 FREE learning resources to make SQL easier to understand!
โ SQL Basics & Queries
โ Joins & Subqueries
โ GROUP BY & Window Functions
โ Hands-On SQL Practice
โ Interview Preparation
๐ฏ Perfect for Students, Freshers & Aspiring Data Analysts!
๐๐ถ๐ป๐ธ๐:-
https://pdlink.in/4CoB4J2
๐ SAVE & SHARE with friends learning SQL! ๐ฅ
Discover 5 FREE learning resources to make SQL easier to understand!
โ SQL Basics & Queries
โ Joins & Subqueries
โ GROUP BY & Window Functions
โ Hands-On SQL Practice
โ Interview Preparation
๐ฏ Perfect for Students, Freshers & Aspiring Data Analysts!
๐๐ถ๐ป๐ธ๐:-
https://pdlink.in/4CoB4J2
๐ SAVE & SHARE with friends learning SQL! ๐ฅ
โค1
๐ ๐๐ป๐ฑ๐ถ๐ฎ ๐๐ ๐ข๐ฝ๐ฒ๐ป | ๐ง๐๐ฟ๐ป ๐๐ผ๐๐ฟ ๐๐ ๐๐ธ๐ถ๐น๐น๐ ๐ถ๐ป๐๐ผ ๐ฎ ๐ฐ๐ฎ๐ฟ๐ฒ๐ฒ๐ฟ ๐ผ๐ฝ๐ฝ๐ผ๐ฟ๐๐๐ป๐ถ๐๐๐
Internships paying up to โน1 lakh/month and full-time roles offering up to โน50 LPA.
๐ โน50,000 for first place ยท โน1.2 lakh in total cash prizes across the top 20
๐ 12โ13 December ยท Round 1 test and interview
๐ฅ๐ฒ๐ด๐ถ๐๐๐ฒ๐ฟ ๐๐ผ๐ฟ ๐๐ฅ๐๐ ๐:-
https://pdlink.in/4APQjZN
๐ Save the dates and share this with a friend whoโs building with AI.
Internships paying up to โน1 lakh/month and full-time roles offering up to โน50 LPA.
๐ โน50,000 for first place ยท โน1.2 lakh in total cash prizes across the top 20
๐ 12โ13 December ยท Round 1 test and interview
๐ฅ๐ฒ๐ด๐ถ๐๐๐ฒ๐ฟ ๐๐ผ๐ฟ ๐๐ฅ๐๐ ๐:-
https://pdlink.in/4APQjZN
๐ Save the dates and share this with a friend whoโs building with AI.
โค3
โจ๏ธ JavaScript Neat Tricks you should know
โค5
๐ง๐ข๐ฃ ๐ฏ ๐๐ฅ๐๐ ๐๐๐ฅ๐ง๐๐๐๐๐๐ง๐๐ข๐ก๐ฆ ๐๐ก ๐๐๐ง๐ ๐ฆ๐๐๐๐ก๐๐ & ๐๐ก๐๐๐ฌ๐ง๐๐๐ฆ! ๐
Kick-start your career in Data Science or Data Analytics
โ Build In-Demand Technical Skills
โ Strengthen Your Resume
โ Explore Certification Opportunities
๐ฏ Perfect for Students, Freshers & Aspiring Data Analysts!
๐๐ถ๐ป๐ธ๐:-
https://pdlink.in/4g3OYGL
๐ SAVE & SHARE with your friends! ๐ฅ
Kick-start your career in Data Science or Data Analytics
โ Build In-Demand Technical Skills
โ Strengthen Your Resume
โ Explore Certification Opportunities
๐ฏ Perfect for Students, Freshers & Aspiring Data Analysts!
๐๐ถ๐ป๐ธ๐:-
https://pdlink.in/4g3OYGL
๐ SAVE & SHARE with your friends! ๐ฅ
๐จ JAVASCRIPT LEARNING SERIES: DAY 10
๐ DOM Manipulation in JavaScript
DOM stands for Document Object Model.
It allows JavaScript to interact with HTML elements and dynamically change the content, structure, and styling of a webpage.
For example, JavaScript can:
โข Change heading text.
โข Change colors and styles.
โข Add or remove HTML elements.
โข Respond to button clicks.
โข Validate form inputs.
โข Make webpages interactive.
Let's understand it step by step.
1๏ธโฃ What is the DOM?
When a browser loads an HTML page, it creates a tree-like structure of elements called the DOM.
Example HTML:
JavaScript can access and modify these elements through the DOM.
2๏ธโฃ Selecting HTML Elements
Before changing an element, we need to select it.
Selects an element using its ID.
Selects the first element matching a CSS selector.
Selects all elements matching a CSS selector.
Other selection methods:
โข
โข
3๏ธโฃ Changing Text Content
JavaScript allows us to change the text inside HTML elements.
The heading will now display:
โข
โข
Example:
Use
4๏ธโฃ Changing CSS Styles
You can dynamically change the styling of an element.
The paragraph's appearance will change instantly.
5๏ธโฃ Adding and Removing CSS Classes
Instead of changing individual styles, you can use
Adds a class if it doesn't exist and removes it if it does.
Checks whether an element has a particular class.
๐ DOM Manipulation in JavaScript
DOM stands for Document Object Model.
It allows JavaScript to interact with HTML elements and dynamically change the content, structure, and styling of a webpage.
For example, JavaScript can:
โข Change heading text.
โข Change colors and styles.
โข Add or remove HTML elements.
โข Respond to button clicks.
โข Validate form inputs.
โข Make webpages interactive.
Let's understand it step by step.
1๏ธโฃ What is the DOM?
When a browser loads an HTML page, it creates a tree-like structure of elements called the DOM.
Example HTML:
<!DOCTYPE html>
<html>
<body>
<h1>Hello World</h1>
<p>Welcome to JavaScript</p>
</body>
</html>
JavaScript can access and modify these elements through the DOM.
2๏ธโฃ Selecting HTML Elements
Before changing an element, we need to select it.
getElementById()Selects an element using its ID.
<h1 id="title">Hello World</h1>
let heading = document.getElementById("title");
console.log(heading);querySelector()Selects the first element matching a CSS selector.
let heading = document.querySelector("#title");
let paragraph = document.querySelector(".description");querySelectorAll()Selects all elements matching a CSS selector.
let items = document.querySelectorAll("p");
console.log(items);Other selection methods:
โข
getElementsByClassName(), Selects elements by class name.โข
getElementsByTagName(), Selects elements by tag name.3๏ธโฃ Changing Text Content
JavaScript allows us to change the text inside HTML elements.
<h1 id="heading">Old Heading</h1>
let heading = document.getElementById("heading");
heading.textContent = "Welcome to JavaScript!";The heading will now display:
Welcome to JavaScript!textContent vs innerHTMLโข
textContent changes or retrieves text.โข
innerHTML reads or changes HTML markup inside an element.Example:
document.getElementById("heading").innerHTML =
"<span>Hello JavaScript</span>";Use
innerHTML carefully with untrusted input because inserting unsafe HTML can create security vulnerabilities.4๏ธโฃ Changing CSS Styles
You can dynamically change the styling of an element.
<p id="message">Hello World</p>
let message = document.getElementById("message");
message.style.color = "blue";
message.style.fontSize = "24px";
message.style.backgroundColor = "lightyellow";The paragraph's appearance will change instantly.
5๏ธโฃ Adding and Removing CSS Classes
Instead of changing individual styles, you can use
classList.add()element.classList.add("active");remove()element.classList.remove("active");toggle()Adds a class if it doesn't exist and removes it if it does.
element.classList.toggle("active");contains()Checks whether an element has a particular class.
console.log(element.classList.contains("active"));6๏ธโฃ Handling Events
Events allow JavaScript to respond to user actions.
Common events include:
โข
โข
โข
โข
โข
โข
Whenever the user clicks the button, the message appears in the console.
7๏ธโฃ Creating New HTML Elements
JavaScript can create HTML elements dynamically.
Here:
โข
โข
โข
8๏ธโฃ Removing HTML Elements
You can remove elements using
The paragraph will be removed from the webpage.
9๏ธโฃ Working with Input Fields
JavaScript can read values entered by users.
The
๐ DOM Traversal
DOM traversal means moving between related HTML elements.
Some useful properties:
โข
โข
โข
โข
โข
โข
1๏ธโฃ1๏ธโฃ Event Object
When an event occurs, JavaScript provides an event object containing information about that event.
Output:
Some useful event properties:
โข
โข
โข
1๏ธโฃ2๏ธโฃ Event Bubbling
Event bubbling means an event triggered on a child element can propagate upward to its parent elements.
For example:
Events allow JavaScript to respond to user actions.
Common events include:
โข
click, When an element is clicked.โข
mouseover, When the pointer moves over an element.โข
mouseout, When the pointer leaves an element.โข
keydown, When a keyboard key is pressed.โข
submit, When a form is submitted.โข
change, When an input value changes.<button id="btn">Click Me</button>
let button = document.getElementById("btn");
button.addEventListener("click", function() {
console.log("Button Clicked!");
});Whenever the user clicks the button, the message appears in the console.
7๏ธโฃ Creating New HTML Elements
JavaScript can create HTML elements dynamically.
let paragraph = document.createElement("p");
paragraph.textContent = "This is a new paragraph.";
document.body.appendChild(paragraph);Here:
โข
createElement() creates a new element.โข
textContent adds its text.โข
appendChild() inserts it into the page.8๏ธโฃ Removing HTML Elements
You can remove elements using
remove().<p id="text">Remove this paragraph</p>
let paragraph = document.getElementById("text");
paragraph.remove();The paragraph will be removed from the webpage.
9๏ธโฃ Working with Input Fields
JavaScript can read values entered by users.
<input type="text" id="username" placeholder="Enter your name">
<button id="submit">Submit</button>
let button = document.getElementById("submit");
button.addEventListener("click", function() {
let name = document.getElementById("username").value;
console.log("Welcome " + name);
});The
.value property retrieves the current value of an input field.๐ DOM Traversal
DOM traversal means moving between related HTML elements.
Some useful properties:
โข
parentElement, Gets the parent element.โข
children, Gets the child elements.โข
firstElementChild, Gets the first child element.โข
lastElementChild, Gets the last child element.โข
nextElementSibling, Gets the next sibling.โข
previousElementSibling, Gets the previous sibling.<div id="parent">
<p>First</p>
<p>Second</p>
</div>
let parent = document.getElementById("parent");
console.log(parent.children);
console.log(parent.firstElementChild);1๏ธโฃ1๏ธโฃ Event Object
When an event occurs, JavaScript provides an event object containing information about that event.
button.addEventListener("click", function(event) {
console.log(event.type);
console.log(event.target);
});Output:
click
<button id="btn">Click Me</button>
Some useful event properties:
โข
event.type, Type of event.โข
event.target, Element that triggered the event.โข
event.preventDefault(), Prevents the browser's default action.1๏ธโฃ2๏ธโฃ Event Bubbling
Event bubbling means an event triggered on a child element can propagate upward to its parent elements.
For example:
<div id="parent">
<button id="child">Click Me</button>
</div>
document.getElementById("parent")
.addEventListener("click", function() {
console.log("Parent Clicked");
});
document.getElementById("child")
.addEventListener("click", function() {
console.log("Child Clicked");
});Clicking the button produces:
Child Clicked
Parent Clicked
This happens because the event bubbles from the child to the parent.
๐ป Real-World Example: Interactive Counter
Let's create a simple counter using DOM manipulation.
<h2 id="count">0</h2>
<button id="increase">Increase</button>
<button id="decrease">Decrease</button>
```javascript
let count = 0;
let display = document.getElementById("count");
document.getElementById("increase")
.addEventListener("click", function() {
count++;
display.textContent = count;
});
document.getElementById("decrease")
.addEventListener("click", function() {
count--;
display.textContent = count;
});
`This example demonstrates how JavaScript updates webpage content based on user interaction.
โก Quick Revision
โข
document โ Represents the webpage.โข
getElementById() โ Selects an element by ID.โข
querySelector() โ Selects the first matching element.โข
querySelectorAll() โ Selects all matching elements.โข
textContent โ Changes or retrieves text.โข
innerHTML โ Changes or retrieves HTML markup.โข
style โ Changes CSS styles.โข
classList โ Manages CSS classes.โข
addEventListener() โ Attaches an event handler.โข
createElement() โ Creates an HTML element.โข
appendChild() โ Adds an element.โข
remove() โ Removes an element.โข
.value โ Gets an input's value.โข
parentElement โ Gets the parent element.๐ก Key Takeaways
โข DOM connects JavaScript with HTML.
โข JavaScript can dynamically modify webpage content and styling.
โข Events make webpages interactive.
โข DOM manipulation is essential for frontend development.
โข Understanding the DOM is a foundation for learning frameworks like React.
๐ Double Tap โค๏ธ For More