Skip to content

Instantly share code, notes, and snippets.

@HelloAlberuni
Last active April 9, 2021 10:58
Show Gist options
  • Save HelloAlberuni/8def5372fdd51a991434620defee1a84 to your computer and use it in GitHub Desktop.
Save HelloAlberuni/8def5372fdd51a991434620defee1a84 to your computer and use it in GitHub Desktop.
Book List Project Using Vanilla JavaScript
// Book constructor
function Book(title, author, isbn){
this.title = title;
this.author = author;
this.isbn = isbn;
}
// UI Constructor
function UI(){}
UI.prototype.addBookToList = function(book){
// Create tr element
let tr = document.createElement('tr');
tr.innerHTML = `
<td>${book.title}</td>
<td>${book.author}</td>
<td>${book.isbn}</td>
<td><a href="#">X</a></td>
`;
// Insert this row to the DOM
let bookList = document.querySelector('#book-list');
};
UI.prototype.clrarFormFields = function(book){
document.querySelector('#title').value = '';
document.querySelector('#author').value = '';
document.querySelector('#isbn').value = '';
};
// Event listener
document.querySelector('#book-form').addEventListener('submit', function(e){
e.preventDefault();
// Get form values
let title = document.querySelector('#title').value,
author = document.querySelector('#author').value,
isbn = document.querySelector('#isbn').value;
// Instantiate book
let book = new Book(title, author, isbn);
// Instantiate UI
let ui = new UI();
// Add book to list
ui.addBookToList(book);
// Clrear form fields
ui.clrarFormFields(book)
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/skeleton/2.0.4/skeleton.css" />
<style>
.success, .error {
color: white;
padding: 5px;
margin: 5px 0 15px 0;
}
.success {
background: green;
}
.error {
background: red;
}
</style>
<title>Book List</title>
</head>
<body>
<div class="container">
<h1>Add Book</h1>
<form id="book-form">
<div>
<label for="title">Title</label>
<input type="text" id="title" class="u-full-width">
</div>
<div>
<label for="author">Author</label>
<input type="text" id="author" class="u-full-width">
</div>
<div>
<label for="isbn">ISBN#</label>
<input type="text" id="isbn" class="u-full-width">
</div>
<div>
<input type="submit" value="Submit" class="u-full-width">
</div>
</form>
<table class="u-full-width">
<thead>
<tr>
<th>Title</th>
<th>Author</th>
<th>ISBN</th>
<th></th>
</tr>
</thead>
<tbody id="book-list"></tbody>
</table>
</div>
<script src="app.js"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment