Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const list = document.querySelector('#book-list ul');
const forms = document.forms;

// delete books
list.addEventListener('click', (e) => {
if(e.target.className == 'delete'){
const li = e.target.parentElement;
li.parentNode.removeChild(li);
}
});

// add books
const addForm = forms['add-book'];
addForm.addEventListener('submit', function(e){
e.preventDefault();

// create elements
const value = addForm.querySelector('input[type="text"]').value;
const li = document.createElement('li');
const bookName = document.createElement('span');
const deleteBtn = document.createElement('span');

// add text content
bookName.textContent = value;
deleteBtn.textContent = 'delete';

// append to DOM
li.appendChild(bookName);
li.appendChild(deleteBtn);
list.appendChild(li);
//list.insertBefore(li, list.querySelector('li:first-child'));
});
48 changes: 48 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link href="styles.css" rel="stylesheet" />
<title>JavaScript DOM Tutorials</title>
</head>
<body>
<div id="wrapper">
<header>
<div id="page-banner">
<h1 class="title">Bookorama</h1>
<p>Books for Ninjas</p>
<form id="search-books">
<input type="text" placeholder="Search books..." />
</form>
</div>
</header>
<div id="book-list">
<h2 class="title">Books to Read</h2>
<ul>
<li>
<span class="name">Name of the Wind</span>
<span class="delete">delete</span>
</li>
<li>
<span class="name">The Wise Man's Fear</span>
<span class="delete">delete</span>
</li>
<li>
<span class="name">Kafka on the Shore</span>
<span class="delete">delete</span>
</li>
<li>
<span class="name">The Master and the Margarita</span>
<span class="delete">delete</span>
</li>
</ul>
</div>
<form id="add-book">
<input type="text" placeholder="Add a book..." />
<button>Add</button>
</form>

</div>
<script src="app.js"></script>
</body>
</html>
138 changes: 138 additions & 0 deletions styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
body{
font-family: Tahoma;
color: #444;
letter-spacing: 1px;
}

h1, h2{
font-weight: normal;
}

#wrapper{
width: 90%;
max-width: 960px;
margin: 20px auto;
border-radius: 6px;
box-shadow: 0px 1px 6px rgba(0,0,0,0.2);
box-sizing: border-box;
padding: 0 0 20px;
overflow: hidden;
border: 1px solid lightgray;
}

#page-banner{
background: #eee ;
padding: 10px 0;

}

#page-banner h1, #page-banner p{
width: 100%;
text-align: center;
margin: 10px 0;
}

#page-banner input{
width: 90%;
max-width: 300px;
margin: 20px auto;
display: block;
padding: 8px;
border: 1px solid #ddd;
border-radius: 4px;
font-size: 16px;
color: #444;
}

#book-list, #add-book, #tabbed-content{
margin: 30px;
}

#book-list ul, #tabbed-content ul{
list-style-type: none;
padding: 0;
}

#book-list li{
padding: 20px;
border-left: 5px solid #ddd;
margin: 20px 10px;
}

#book-list li:hover{
border-color: #9361bf;
}

.delete{
float: right;
background: #9361bf;
padding: 6px;
border-radius: 4px;
cursor: pointer;
color: white;
}

.delete:hover{
background: #333;
}

#add-book{
width: 400px;
margin: 0 auto;
}

#add-book input{
display: block;
margin: 20px 0;
padding: 10px;
border: 1px solid #ccc;
font-size: 16px;
border-radius: 4px 0 0 4px;
box-sizing: border-box;
width: 300px;
float: left;
}

#add-book button{
border: 1px solid #9361bf;
background: #9361bf;
padding: 10px 20px;
font-size: 16px;
display: inline-block;
margin: 0;
border-radius: 0 4px 4px 0;
cursor: pointer;
width: 100px;
float: left;
margin: 20px 0;
border-left: 0;
color: white;
}

#add-book:after{
content: '';
display: block;
clear: both;
}

/*
#tabbed-content li{
display: inline-block;
padding: 10px 14px;
background: #ddd;
border-radius: 4px;
cursor: pointer;
margin-right: 10px;
}

#tabbed-content .tab{
display: none;
border: 1px solid #ddd;
padding: 0 10px;
border-radius: 4px;
}

#tabbed-content .tab.active{
display: block;
}
*/