-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e6027c4
commit 85168d9
Showing
3 changed files
with
87 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Document</title> | ||
</head> | ||
<body> | ||
|
||
<table id="myTable" border="1"> | ||
<thead> | ||
<tr> | ||
<th>Title</th> | ||
<th>Author</th> | ||
<th>Pages</th> | ||
<th>Read</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
</tbody> | ||
</table> | ||
|
||
|
||
<form onsubmit="handleSubmit(event)" id="bookForm"> | ||
<input type="text" id="title"> | ||
<input type="text" id="author"> | ||
<input type="text" id="pages"> | ||
<input type="text" id="read"> | ||
<input type="submit"> | ||
</form> | ||
|
||
|
||
|
||
<script src="script.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
|
||
const myLibrary = []; | ||
|
||
function Book(title, author, pages, read) { | ||
this.title = title; | ||
this.author=author; | ||
this.pages= pages; | ||
this.read=read; | ||
} | ||
|
||
|
||
function addBook(title, author, pages, read) { | ||
myLibrary.push(new Book(title, author, pages, read)) | ||
} | ||
|
||
function removeBook(index) { | ||
myLibrary.splice(index,1) | ||
renderLibrary() | ||
} | ||
|
||
function handleSubmit(event) { | ||
event.preventDefault(); | ||
const title = document.getElementById('title').value ; | ||
const author = document.getElementById('author').value ; | ||
const pages = document.getElementById('pages').value ; | ||
const read = document.getElementById('read').value ; | ||
|
||
if (!title || !author || !pages || !read) { | ||
alert("all fields much be filled out."); | ||
return; | ||
} | ||
|
||
addBook(title, author, pages, read); | ||
renderLibrary() | ||
document.getElementById('bookForm').reset(); | ||
} | ||
|
||
|
||
|
||
console.log(myLibrary); | ||
|
||
function renderLibrary() { | ||
document.querySelector('#myTable tbody').innerHTML = myLibrary.map((book, index) => `<tr> | ||
<td>${book.title}</td> | ||
<td>${book.author}</td> | ||
<td>${book.pages}</td> | ||
<td>${book.read}</td> | ||
<td><button onclick="removeBook(${index})">Remove</button></td> | ||
</tr>` | ||
).join('') | ||
} |