-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDomAddingNewElemets.js
More file actions
84 lines (60 loc) · 2.77 KB
/
DomAddingNewElemets.js
File metadata and controls
84 lines (60 loc) · 2.77 KB
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
//classList
const sectionClass=document.querySelector(".todosection")
console.log(sectionClass)
console.log(sectionClass.classList);
//adding class using javacript
sectionClass.classList.toggle('bg-dark'); //dont put . seriously i wasted so much of my time
//Adding html element
//bad way
const todolist= document.querySelector('.todosection');
console.log(todolist.innerHTML);
// todolist.innerHTML+=`<li class= ".bg-dark"> New Todo List </li>`; ??//??
//to remove class
// todolist.classList.remove('container');
//to find if call exist
console.log(todolist.classList.contains('container'));
const header=document.querySelector('.header')
console.log(header); // header.classList.add('bg-dark')
//adding todoList //BAD WAY
const newTodoList=document.querySelector('.todo-list');
newTodoList.innerHTML+='<li> created by bad method </li>'; //bad practice
//GOOD WAY
const newtoDolist=document.querySelector('.todo-list');
//document.createNewTextNode("this is long method")
let newlist=document.createElement('li')
newlist.textContent="this was probably the best method";
newTodoList.append(newlist); //prepend will pre add it
//to remove an element //item name.remove();
// newlist.remove();
//before and after addling element as sibling
newlist=document.createElement('li')
newlist.textContent="using before method";
newTodoList.before(newlist);
//beforeend afterend beforebegin afterbegin
newElement=document.querySelector('.todo-list');
newElement.insertAdjacentHTML("beforeend","<li>using before end</li>")
newElement.insertAdjacentHTML("afterend","<li>using After end</li>")
newElement.insertAdjacentHTML("afterbegin","<li>using After begin</li>")
//Clone Nodes
newElement=document.querySelector('.todo-list');
const newli=document.createElement('li');
newli.textContent="______new todo main______";
//clone
const newli2=newli.cloneNode(true); //true will deep clone (inner elements)
newElement.append(newli);
newElement.prepend(newli2);
//__________________________________OLD BROWSER CODE HERE__________________________________
//to insert before the main element using inserBefore
newElement=document.querySelector('.todo-list');
const li=document.createElement('li');
//reference element
const referenceNode=document.querySelector(".first-todo")
li.textContent="new first";
newElement.insertBefore(li,referenceNode); //(insert to be inserted,reference element)
//replace child
newElement.replaceChild(li, referenceNode); // will replace the main element
//remove child
newElement.removeChild(li);
// newElement=document.querySelector('.todo-list');
// const newli3=document.createElementelement(li);
// _______________________________________END_____________________________________________