-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDOM.js
63 lines (48 loc) · 1.75 KB
/
DOM.js
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
//Dom
//single element selector
console.log(document.getElementById('my-form'));
console.log(document.querySelector('email'));
//multiple elements selector
console.log(document.querySelector('container'));
console.log(document.getElementsByClassName('btn'));
console.log(document.getElementsByTagName('button'));
const ul = document.querySelector('.items');
//ul.remove();
ul.lastElementChild.remove();
ul.firstElementChild.textContent = "Hello";
ul.lastElementChild.innerHTML = `<h1> Hello </h1>`;
ul.children[0].innerHTML = `Item 1`;
//evenet listner
const btn = document.querySelector('.btn');
btn.style.backgroundColor ='red';
// btn.addEventListener('click', (e) => {
// e.preventDefault();
// document.querySelector('body').classList.add('bg-dark');
// });
const myForm = document.querySelector('#my-form');
const inputName = document.querySelector('#name');
const inputEmail = document.querySelector('#email');
const msg = document.querySelector('.msg');
const userList = document.querySelector('#users');
myForm.addEventListener('submit', onsubmit);
function onsubmit(e){
e.preventDefault();
if (inputName.value === '' || inputEmail.value === ''){
msg.classList.add('error');
msg.innerHTML = 'plz fill the blanks';
setTimeout(() => msg.remove() , 3000);
}
else{
// Create new list item with user
const li = document.createElement('li');
// Add text node with input values
li.appendChild(document.createTextNode(`${inputName.value} : ${inputEmail.value}`));
// Add HTML
// li.innerHTML = `<strong>${nameInput.value}</strong>e: ${emailInput.value}`;
// Append to ul
userList.appendChild(li);
// Clear fields
inputName.value = '';
inputEmail.value = '';
}
}