Skip to content

Commit 588b1a8

Browse files
⚡️ Lesson-14: About Attribues set,get,remove etc.
1 parent 63767bc commit 588b1a8

File tree

3 files changed

+254
-0
lines changed

3 files changed

+254
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
//Delete Books
2+
const lists = document.querySelector('#book-list ul')
3+
4+
lists.addEventListener('click', function (e) {
5+
if (e.target.className == 'delete') {
6+
const li = e.target.parentNode
7+
lists.removeChild(li)
8+
}
9+
})
10+
11+
12+
//add book-lits
13+
14+
const addBooks = document.forms['add-book'];
15+
16+
addBooks.addEventListener('submit', function (e) {
17+
e.preventDefault();
18+
const value = addBooks.querySelector('input[type="text"]').value
19+
console.log(value)
20+
21+
//Add ELements
22+
const li = document.createElement('li')
23+
const bookName = document.createElement('span')
24+
const deleteBtn = document.createElement('span')
25+
26+
//add content
27+
28+
bookName.textContent = value
29+
deleteBtn.textContent = 'delete'
30+
31+
//Styles and CLasses by classList
32+
33+
bookName.classList.add('book')
34+
deleteBtn.classList.add('delete')
35+
36+
//Append these Elements to document
37+
li.appendChild(bookName);
38+
li.appendChild(deleteBtn);
39+
lists.appendChild(li)
40+
41+
})
42+
43+
var book = document.querySelector('li:first-child .name')
44+
console.log(book)
45+
46+
//Getting ClassName
47+
48+
console.log(book.getAttribute('class'))
49+
50+
//Getting href if any
51+
52+
console.log(book.getAttribute('href'))
53+
54+
//Setting Attribute
55+
56+
console.log(book.setAttribute('class', 'name-2'))
57+
console.log(book)
58+
59+
//Checking if a Node has a particular Attribute.
60+
console.log(book.hasAttribute('class'))
61+
62+
//Removing attribute
63+
console.log(book.removeAttribute('class'))
64+
console.log(book)
65+
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<link rel="stylesheet" href="style.css">
7+
<title>DOM Introduction</title>
8+
</head>
9+
<body>
10+
<div id="wrapper">
11+
<header>
12+
<div id="page-banner">
13+
<h1 class="title">Books</h1>
14+
<form id="search-books">
15+
<input type="text" placeholder="Search books..." />
16+
</form>
17+
</div>
18+
</header>
19+
<div id="book-list">
20+
<h2 class="title">Books for Reading</h2>
21+
<ul>
22+
<li>
23+
<span class="name">Book 1</span>
24+
<span class="delete">delete</span>
25+
</li>
26+
<li>
27+
<span class="name">Book 2</span>
28+
<span class="delete">delete</span>
29+
</li>
30+
<li>
31+
<span class="name">Book 3</span>
32+
<span class="delete">delete</span>
33+
</li>
34+
<li>
35+
<span class="name">Book 4</span>
36+
<span class="delete">delete</span>
37+
</li>
38+
</ul>
39+
</div>
40+
<form id="add-book">
41+
<input type="text" placeholder="Add a book..." />
42+
<button>Add</button>
43+
</form>
44+
45+
</div>
46+
<script src="app.js"></script>
47+
</body>
48+
</html>
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
body {
2+
font-family: Tahoma;
3+
color: #444;
4+
letter-spacing: 1px;
5+
}
6+
7+
h1,
8+
h2 {
9+
font-weight: normal;
10+
}
11+
12+
#wrapper {
13+
width: 90%;
14+
max-width: 960px;
15+
margin: 20px auto;
16+
border-radius: 6px;
17+
box-shadow: 0px 1px 6px rgba(0, 0, 0, 0.2);
18+
box-sizing: border-box;
19+
padding: 0 0 20px;
20+
overflow: hidden;
21+
border: 1px solid lightgray;
22+
}
23+
24+
#page-banner {
25+
background: #eee;
26+
padding: 10px 0;
27+
}
28+
29+
#page-banner h1,
30+
#page-banner p {
31+
width: 100%;
32+
text-align: center;
33+
margin: 10px 0;
34+
}
35+
36+
#page-banner input {
37+
width: 90%;
38+
max-width: 300px;
39+
margin: 20px auto;
40+
display: block;
41+
padding: 8px;
42+
border: 1px solid #ddd;
43+
border-radius: 4px;
44+
font-size: 16px;
45+
color: #444;
46+
}
47+
48+
#book-list,
49+
#add-book,
50+
#tabbed-content {
51+
margin: 30px;
52+
}
53+
54+
#book-list ul,
55+
#tabbed-content ul {
56+
list-style-type: none;
57+
padding: 0;
58+
}
59+
60+
#book-list li {
61+
padding: 20px;
62+
border-left: 5px solid #ddd;
63+
margin: 20px 10px;
64+
}
65+
66+
#book-list li:hover {
67+
border-color: #9361bf;
68+
}
69+
70+
.delete {
71+
float: right;
72+
background: #9361bf;
73+
padding: 6px;
74+
border-radius: 4px;
75+
cursor: pointer;
76+
color: white;
77+
}
78+
79+
.delete:hover {
80+
background: #333;
81+
}
82+
83+
#add-book {
84+
width: 400px;
85+
margin: 0 auto;
86+
}
87+
88+
#add-book input {
89+
display: block;
90+
margin: 20px 0;
91+
padding: 10px;
92+
border: 1px solid #ccc;
93+
font-size: 16px;
94+
border-radius: 4px 0 0 4px;
95+
box-sizing: border-box;
96+
width: 300px;
97+
float: left;
98+
}
99+
100+
#add-book button {
101+
border: 1px solid #9361bf;
102+
background: #9361bf;
103+
padding: 10px 20px;
104+
font-size: 16px;
105+
display: inline-block;
106+
margin: 0;
107+
border-radius: 0 4px 4px 0;
108+
cursor: pointer;
109+
width: 100px;
110+
float: left;
111+
margin: 20px 0;
112+
border-left: 0;
113+
color: white;
114+
}
115+
116+
#add-book:after {
117+
content: "";
118+
display: block;
119+
clear: both;
120+
}
121+
122+
#tabbed-content li {
123+
display: inline-block;
124+
padding: 10px 14px;
125+
background: #ddd;
126+
border-radius: 4px;
127+
cursor: pointer;
128+
margin-right: 10px;
129+
}
130+
131+
#tabbed-content .tab {
132+
display: none;
133+
border: 1px solid #ddd;
134+
padding: 0 10px;
135+
border-radius: 4px;
136+
}
137+
138+
#tabbed-content .tab.active {
139+
display: block;
140+
}
141+

0 commit comments

Comments
 (0)