FSD Module 2 Notes Partial
FSD Module 2 Notes Partial
1. By ID (getElementById)
javascript
CopyEdit
var firstItem = document.querySelector("li.hot");
1. By Class (getElementsByClassName)
1
Full Stack Development-BIS601 Dr. Aravinda Thejas Chandra Dept of ISE SJCIT Chickballapur
javascript
CopyEdit
var allHotItems = document.querySelectorAll("li.hot");
Property Description
parentNode Moves to the parent element
firstChild Selects the first child node
lastChild Selects the last child node
previousSibling Moves to the previous sibling node
nextSibling Moves to the next sibling node
Example:
• Using innerHTML (Includes HTML inside the element, but can pose security
risks):
b. Updating Attributes
• Get an attribute:
• Set an attribute:
document.getElementById("myLink").setAttribute("href",
"https://example.com");
2
Full Stack Development-BIS601 Dr. Aravinda Thejas Chandra Dept of ISE SJCIT Chickballapur
document.getElementById("header").style.color = "blue";
document.getElementById("header").style.fontSize = "24px";
Steps:
Example:
document.getElementById("btn").onclick = function() {
alert("Button Clicked!");
};
document.getElementById("btn").addEventListener("click", function() {
3
Full Stack Development-BIS601 Dr. Aravinda Thejas Chandra Dept of ISE SJCIT Chickballapur
alert("Button Clicked!");
});
Event Description
click User clicks an element
mouseover Mouse hovers over an element
keydown Key is pressed
submit Form is submitted
Example:
document.getElementById("myInput").addEventListener("keydown",
function(event) {
console.log("Key Pressed: " + event.key);
});
8. Summary
✔ The DOM represents the structure of an HTML document in memory.
✔ JavaScript allows access, modification, and traversal of DOM elements.
✔ Events make web pages interactive.
✔ Use safe practices (e.g., textContent over innerHTML) to prevent security risks.