I want to be able to write the title of a book for now, then have it save to an array. When I click add book it should pop up right below the example text on the page, however im not sure why my functions are not working. Im new to this so its a bit confusing.
<title></title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="container">
<h1>Library</h1>
<button type="button" onclick="addBookToLibrary();" id="addButton">Add
A Book</button>
<div class="formContainer">
<form id="info">
<input type="text" placeholder="Title" id="title">
<input type="text" placeholder="Author"
id="author">
<input type="number" placeholder="#Pages" min="0" max="5000"
id="number">
<label for="read">Read?</label>
<input type="checkbox" id="read">
</form>
</div>
</div>
<hr>
<div class="bookContainer">
<div class="wrapper">
<h2>Title</h2>
<h2>Author</h2>
<h2>Pages</h2>
<h2>Read</h2>
</div>
<div class="books">
<p class="item">The Hobbit</p>
<p class="item">by J.R.R. Tolkien</p>
<p class="item">295 pages</p>
<p class="item">Read</p>
</div>
</div>
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
#addButton {
margin-bottom: 1em;
}
.container {
text-align: center
}
.container h1 {
margin-top: .5em;
margin-bottom: .5em;
}
hr {
margin-top: 2em;
margin-bottom: 2em;
}
.bookContainer{
width: 90%;
border:2px solid black;
margin: auto;
}
.wrapper {
display: flex;
justify-content: space-around;
}
.books {
display: flex;
justify-content: space-around;
margin-top: 1em;
}
.item {
flex:1;
text-align:center;
}
#number {
width: 8%;
}
let myLibrary = [];
function book(title, author, pages, read) {
this.title = title
this.author = author
this.pages = pages
this.read = read
this.info = function() {
console.log(title, author, pages, read);
}
}
function addBookToLibrary() {
// get value from the input text
let title = document.querySelector('#title').value;
//append data to the array myLibrary
myLibrary.push(title);
let pval = '';
for (i = 0; i < myLibrary.length; i++) {
pval = pval + myLibrary[i] + '<br/>';
}
}
function render() {
//display array data
document.querySelector('.wrapper').innerHTML = pval;
}
I didnt add the html bc i didnt want it to be super long, but if you need it i can always add it.