-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtut16.js
More file actions
70 lines (50 loc) · 1.89 KB
/
tut16.js
File metadata and controls
70 lines (50 loc) · 1.89 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
// Creating , Removing, Replacing elements
console.log('welcome to tut16');
// Creating Element
let myElement = document.createElement('li');
// console.log(myElement);
// Add class name, id, text, attribute etc. to created element
// myElement.className = "list-unstyled mt-3 mb-4";
myElement.id = "newItem1";
myElement.setAttribute('title', 'MyTitle');
//Adding text to new element - method -1
myElement.innerHTML = "<b>Database Support - new element</b>"
console.log(myElement);
//Adding text to new element - method -2
let text = document.createTextNode("My new text node");
myElement.appendChild(text);
console.log(myElement);
// selecting <ul> element which class is "list-unstyled"
let ul = document.querySelector('ul.list-unstyled');
// add new element to existing element
ul.appendChild(myElement);
// console.log(ul);
let myElement2 = document.createTextNode("2nd element");
myElement.replaceWith(myElement2);
// Replacing existing element
let myUL = document.getElementById("myUL");
let myElement3 = document.createTextNode("3rd element");
console.log(myUL);
myUL.replaceChild(myElement3, document.getElementById('myli'));
// Removing element
ul.removeChild(document.getElementById('myli2'));
// get attribute
let a = ul.getAttribute('id');
console.log(a);
a = ul.getAttribute('class');
console.log(a);
a = ul.hasAttribute('href');
console.log(a);
a = ul.removeAttribute('id');
console.log(ul);
// Solved Quiz - Create Header element and a href
let myH1 = document.createElement('h4');
text = document.createTextNode("Go to CodeWithHarry");
myH1.appendChild(text);
console.log(myH1);
let pricing_header = document.querySelector('h1.display-4');
pricing_header.appendChild(myH1);
console.log(pricing_header);
let myhref = document.createElement('a');
myhref.setAttribute('href', 'https://www.codewithharry.com');
myH1.append(myhref);