-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdom_modification.html
More file actions
92 lines (76 loc) · 3.04 KB
/
dom_modification.html
File metadata and controls
92 lines (76 loc) · 3.04 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
82
83
84
85
86
87
88
89
90
91
92
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
<title>Item Lister</title>
</head>
<body>
<header id="main-header" class="bg-success text-white p-4 mb-3">
<div class="container">
<h1 id="header-title">Item Lister <span style="display:none">123</span></h1>
</div>
</header>
<div class="container">
<div id="main" class="card card-body">
<h2 class="title">Add Items</h2>
<form class="form-inline mb-3">
<input type="text" class="form-control mr-2">
<input type="submit" class="btn btn-dark" value="Submit">
</form>
<h2 class="title">Items</h2>
<ul id="items" class="list-group">
<li class="list-group-item">Item 1</li>
<li class="list-group-item">Item 2</li>
<li class="list-group-item">Item 3</li>
<li class="list-group-item">Item 4</li>
</ul>
<span></span>
</div>
</div>
<script>
var ele = document.querySelector('#items');
//console.log(ele.parentNode);
//ele.parentNode.style.backgroundColor = '#f4f4f4';
//console.log(ele.parentNode.parentNode);
// parentNode and parentElement can be used interchangibly
// text Node represents a line break or whitespace
//console.log(ele.childNodes);
// use children instead of childNode , it avoids text nodes
//console.log(ele.children);
//console.log(ele.children[1]);
//ele.children[1].style.backgroundColor = 'yellow';
//firstChild return the text node as well so use firstElementChild
//console.log(ele.firstChild);
//console.log(ele.firstElementChild);
//ele.firstElementChild.textContent = 'hello';
//lastChild return the text node as well so use lastElementChild
//console.log(ele.lastChild);
//console.log(ele.lastElementChild);
//ele.lastElementChild.textContent = 'hello again';
// nextSibling and nextElementSibling
//console.log(ele.nextSibling);
//console.log(ele.nextElementSibling);
//console.log(ele.previousSibling);
//console.log(ele.previousElementSibling);
//ele.previousElementSibling.style.color = 'green';
var newdiv = document.createElement('div');
newdiv.className = 'hello';
newdiv.id = 'hello1';
newdiv.setAttribute('title','i am div');
var divText = document.createTextNode('Hello World');
newdiv.appendChild(divText);
console.log(newdiv);
var container = document.querySelector('header .container');
var h1 = document.querySelector('header h1');
container.insertBefore(newdiv,h1);
//var newLi = document.createElement('div');
//newdiv.className = 'hello';
let liContainer = document.querySelector('.list-group');
let li = document.querySelector('li:nth-child(1)');
liContainer.insertBefore(newdiv,li);
</script>
</body>
</html>