forked from Kashyap-Rishi/Gdsc_practice-site
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
102 lines (53 loc) · 2.22 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
var heading = document.getElementById("myHeading");
var paragraphs = document.getElementsByClassName("myClass");
var listItems = document.getElementsByTagName("li");
var button = document.querySelector("#myButton");
var allParagraphs = document.querySelectorAll("p.myClass");
var elementsByName = document.getElementsByName("myName");
var link = document.getElementById("myLink");
// Modifying Elements
// heading.innerHTML = "Hello, DOM!";
// paragraphs[0].textContent = "Updated paragraph text";
// button.style.backgroundColor = "blue";
// Creating and Appending Elements
// var newParagraph = document.createElement("p");
// newParagraph.textContent = "New paragraph added!";
// document.body.appendChild(newParagraph);
// Removing Elements
// var list = document.querySelector("ul");
// var itemToRemove = listItems[1];
// list.removeChild(itemToRemove);
// Traversal and Relationships
// var firstChild = document.querySelector("ul").firstElementChild;
// var lastChild = document.querySelector("ul").lastElementChild;
// var parentElement = document.getElementById("myButton").parentNode;
// var nextSibling = document.querySelector("h1").nextElementSibling;
// var prevSibling = document.querySelector("p").previousElementSibling;
// Attributes
// var hasDataAttribute = heading.hasAttribute("data-id");
// heading.setAttribute("data-id", "12345");
// var getDataAttribute = heading.getAttribute("data-id");
// heading.removeAttribute("data-id");
// Appending a new child element
// var list = document.querySelector("ul");
// var newItem = document.createElement("li");
// newItem.textContent = "New Item";
// list.appendChild(newItem);
// Replacing an element with another node
// var oldItem = document.querySelector("ul li");
// var newItem = document.createElement("li");
// newItem.textContent = "Replaced Item";
// oldItem.replaceWith(newItem);
// Event Listeners
button.addEventListener("click", function() {
alert("Button clicked!");
});
link.addEventListener("mouseenter", function() {
link.style.color = "red";
});
link.addEventListener("mouseleave", function() {
link.style.color = "blue";
});
// listItems[0].addEventListener("click", function() {
// alert("List item clicked!");
// });