-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertPractice.html
More file actions
58 lines (50 loc) · 1.7 KB
/
insertPractice.html
File metadata and controls
58 lines (50 loc) · 1.7 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Insert Practice</title>
<link rel="stylesheet" href="style.css">
<style>
.container{
display: block;
}
</style>
</head>
<body>
<div class="container">
<div class="content">
<h3>What is this</h3>
<h4>I dont want this</h4>
</div>
</div>
<script>
const content = document.querySelector('.content');
//Replace contents of an html tag using innerHTML
content.innerHTML = '<h1 id="stuff">Stuff was changed</h1><h4>This was also changed</h4><h3 class="unexisting">This does not exist</h3>';
const added = document.createElement('h1');
added.innerHTML = 'Stuff was added';
content.appendChild(added);
content.appendChild(added);
const addedToo = document.createElement('h2');
const addedThen = document.createElement('h3');
const addedIGuess = document.createElement('h4');
addedToo.innerHTML = 'But then this was added';
addedThen.innerHTML = 'and then this too';
addedIGuess.innerHTML = 'maybe this too, i guess';
content.appendChild(addedToo);
content.appendChild(addedThen);
content.appendChild(addedIGuess);
//put this at the top in addition to existing elements
const food = document.createElement('h1');
food.innerHTML = 'Pizza exists';
content.insertAdjacentElement('afterend', food);
//to remove an element
const unwantedElement = document.querySelector('h4');
unwantedElement.remove();
const uneededElement = document.querySelector('#stuff');
uneededElement.remove();
const dislikedElement = document.querySelector('.unexisting');
dislikedElement.remove();
</script>
</body>
</html>