1+ <!DOCTYPE html>
2+ < html lang ="en ">
3+ < head >
4+ < meta charset ="UTF-8 ">
5+ < title > DOM Learning</ title >
6+ < style >
7+ .bg-black {
8+ background-color : black;
9+ color : white;
10+ }
11+ </ style >
12+ </ head >
13+ < body class ="bg-black ">
14+ < div >
15+ < h1 id ="title " class ="heading "> DOM Learning < span style ="display: none; "> test text</ span > </ h1 >
16+ < h2 > Lorem ipsum dolor sit</ h2 >
17+ < h2 > Lorem ipsum dolor sit</ h2 >
18+ < h2 > Lorem ipsum dolor sit</ h2 >
19+ < p > Lorem ipsum dolor sit amet.</ p >
20+
21+ < ul >
22+ < li class ="li-item "> one</ li >
23+ < li class ="li-item "> two</ li >
24+ < li class ="li-item "> three</ li >
25+ </ ul >
26+ </ div >
27+ </ body >
28+ </ html >
29+ <!--
30+ document.getElementById('title').getAttribute('id')
31+ document.getElementById('title').setAttribute('class','test')
32+ This overrides current value of class.
33+ To include both class and test, use the following code:
34+ document.getElementById('title').setAttribute('class', document.getElementById('title').getAttribute('class') + ' test');-->
35+
36+ <!--
37+ We can set different styles as well like:
38+ title.style.backgroundColor= 'green'
39+ title.style.padding= "15px"
40+ title.style.borderRadius="15px"-->
41+
42+ <!--
43+ There is a difference between innerText and textContent
44+ innerText returns the visible text, while textContent returns all text including hidden elements.
45+ innerHTML gives html content-->
46+
47+ <!--
48+ While accessing the classes, ids, we usually get HTML Collection, not an array or a single elemwent.
49+ To convert HTML Collection to an array, we can use:
50+ Array.from(document.getElementsByClassName('className'))
51+
52+ querySelector and querySelectorAll are also useful.
53+ document.querySelector('.className') // returns first element with className
54+
55+ const myul=document.querySelector('ul')
56+ const turnGreen= myul.querySelector('li')
57+ turnGreen.style.backgroundColor= "green"
58+
59+ While using querySelectorAll, we get a node list
60+
61+ const myul=document.querySelector("ul")
62+ undefined
63+
64+ myul.querySelectorAll("li")
65+ NodeList(3) [li, li, li]0: li1: li2: lilength: 3[[Prototype]]: NodeListentries: ƒ entries()forEach: ƒ forEach()item: ƒ item()keys: ƒ keys()length: (...)values: ƒ values()constructor: ƒ NodeList()Symbol(Symbol.iterator): ƒ values()Symbol(Symbol.toStringTag): "NodeList"get length: ƒ length()[[Prototype]]: Object
66+
67+ const liList= myul.querySelectorAll("li")
68+ undefined
69+
70+ liList.style.color= "green"
71+ VM1333:1 Uncaught TypeError: Cannot set properties of undefined (setting 'color')
72+ at <anonymous>:1:19
73+ (anonymous) @ VM1333:1
74+
75+ liList[0].style.color= "green"
76+ 'green'
77+
78+ So, we can access each element in the NodeList and set styles or attributes as needed.
79+
80+ const listItem=document.getElementByClassName("li-item")
81+ If we try to access a class using getElementByClassName, it will return an HTML Collection, not a single element.
82+ We cannot access or traverse through
83+
84+ To traverse through the HTML Collection, we can use a for loop or Array.from() method:
85+
86+ const liclass= document.getElementsByClassName("li-item")
87+ for(let i=0;i<liclass.length;i++){
88+ console.log(liclass[i])
89+ }
90+
91+ Array.from(liclass).forEach((item) => {
92+ console.log(item);
93+ });
94+ const convArray= Array.from(liclass)
95+ convArray.forrEach(function(li){
96+ li.style.color= "green"
97+ })
98+ -->
0 commit comments