-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathindex.js
More file actions
56 lines (43 loc) · 1.39 KB
/
index.js
File metadata and controls
56 lines (43 loc) · 1.39 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
// Input Value Example
let inputValue = '';
document.getElementById('myInput').addEventListener('change', function(event){
inputValue = event.target.value
console.log(inputValue);
});
// Event Bubbling Example
const parent = document.querySelector('.parent');
const child = document.querySelector('.child');
parent.addEventListener('click', function(){
console.log('parent was clicked');
});
// not stopping the bubbling effect
child.addEventListener('click', function(){
console.log('child was clicked');
});
// stopping the bubbling effect
child.addEventListener('click', function(event){
event.stopPropagation();
console.log('child was clicked');
});
// Form preventDefault Example
const formBtn = document.getElementById('form-btn');
formBtn.addEventListener('click', function(event){
// prevent default event
event.preventDefault();
// stop event bubbling
event.stopPropagation();
console.log('stopped form submission')
});
// Create Element And Append Child Methods
// create the button element
const btn = document.createElement('BUTTON');
// add inner text
btn.innerText = 'Click me';
// Add the new button into the body element
document.body.appendChild(btn);
// Parent Node
const btnParent = child.parentNode;
console.log(btnParent);
// Get Attribute & Set Attribute
const btnClass = document.querySelector('.btn').getAttribute('class');
console.log(btnClass)