-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsix_editRemoveElements.html
51 lines (44 loc) · 1.8 KB
/
six_editRemoveElements.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body style="background-color: #212121; color: #fff;">
<ul class="language">
<li>JavaScript</li>
</ul>
</body>
<script>
// (i) Creating List Item
function addLanguageName(langName) {
const li = document.createElement('li') // Element created
li.innerHTML = `${langName}` // Adding value
document.querySelector('.language').appendChild(li) // Attaching
}
addLanguageName("Python") // Python will be added under Javascript
// (ii) Optipmized Method
function addOptiLanguage(langName) {
const li = document.createElement('li') // Element created
li.appendChild(document.createTextNode(langName)) // Adding value
document.querySelector('.language').appendChild(li) // Attaching
}
addOptiLanguage("Java")
//---------------------------------------------------------------------------------------
// (i) Edit
const secondLang = document.querySelector('li:nth-child(2)') // Selected 2nd list item
// secondLang.innerHTML = "C++" // Not the optimized approach for bigger projects.
// (ii) Edit
const newli = document.createElement('li') // Element created
newli.textContent = "C++"
secondLang.replaceWith(newli) // Python will be replaced with C++
// (iii) Edit
const firstLang = document.querySelector('li:first-child')
firstLang.outerHTML = ('<li>Typescript</li>')
//---------------------------------------------------------------------------------------
// Remove
const lastLang = document.querySelector('li:last-child')
lastLang.remove() // Last list item with text java will be removed.
</script>
</html>