I am trying to create a simple "book keeping" web application as specified in The Odin Project. My problem is that when I click the "submit" button in my form, the data is sent to the array but then disappears immediately out of it. Running the "addBookToLibrary" function in the js file itself works just fine, but running it via the onclick
is problematic.
I'm including the HTML and js.
Any help is appreciated as I can't see why the function works when called in the js file but not when called through the HTML onclick
method!
let myLibrary = []
const table = document.querySelector("#libraryTable")
//constructs the book prototype
function Book(title, author) {
this.title = title;
this.author = author;
}
//creates the newBook and pushes it to the library
function addBookToLibrary(title, author) {
let newBook = new Book(title, author)
myLibrary.push(newBook)
render()
}
//a starter book
addBookToLibrary("The Hobbit", "Tolkien")
addBookToLibrary("The Hobbit", "Tolkien")
addBookToLibrary("The Hobbit", "Tolkien")
function render() {
book = myLibrary[myLibrary.length - 1]
table.innerHTML += `<tr class="bookEntry"><td>${book.title}</td><td>${book.author}</td>`
}
addButton = document.querySelector("#submitButton")
function addyWaddy() {
addBookToLibrary(newBookForm[0].value, newBookForm[1].value);
};
<div id="container">
<div class="grid-item" id="leftSide">
<h1>Add books here</h1>
<form action="" id="newBookForm">
<input type="text" id="newTitle" value="Insert book title" name="titleName"></input>
<input type="text" id="newAuthor" value="Insert book author" name="authorName"></input>
<br>
<input type="submit" id="submitButton" value="Submit" onclick="addyWaddy()">
</form>
</div>
<div class="grid-item" id="rightSide">
<h1>Books go here</h1>
<table id="libraryTable">
<tr>
<th>Title</td>
<th>Author</td>
</tr>
</table>
</div>
</div>