I need to make a simple project in javascript, we need to create a single library with javascript objects and let the user to add new books. I requested user data with a form tag in html and created new objects and I stored them inside a single array called library. The books are showed with no problem in the DOM the thing is that I need a button that deletes an specific book, I created a single button but it only deletes the first book in the array. I hope you can help me.
HTML:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="./styles.css" />
<title>Document</title>
</head>
<body>
<h1>My Library</h1>
<input id="title" type="text" placeholder="Book Title">
<input id="author" type="text" placeholder="Book Author">
<input id="date" type="text" placeholder="Publish Date">
<select id="read" name="read">
<option value="yes">yes</option>
<option value="no">no</option>
</select>
<input type="button" value="New Book" onclick="add_book()">
<div id="display"></div>
<script src="app.js"></script>
</body>
</html>
------------------------------------------------------------------------------------------------
JAVASCRIPT:
var library = [];
var title_input = document.getElementById("title");
var author_input = document.getElementById("author");
var date_input = document.getElementById("date");
var read_input = document.getElementById("read");
function Book(title, author, date, read) {
this.title = title;
this.author = author;
this.date = date
this.read = read
};
function add_book() {
var newBook = new Book(title_input, author_input, date_input, read_input)
library.push(`Title: ${newBook.title.value} <br>`+`Author: ${newBook.author.value} <br>`+
`Realease date: ${newBook.date.value} <br>`+`Readed: ${newBook.read.value} <br>` )
show_library();
};
function delete_book(arr, elem){
index = arr.indexOf(elem);
arr.splice(elem,1);
show_library();
}
function show_library() {
document.getElementById("display").innerHTML = "";
for(i = 0; i<library.length; i++){
document.getElementById("display").innerHTML += library[i]+
'<button onclick="delete_book(library, library[i]);">Delete</button><br>';
}
};
You've written library[i]
as plain text into the DOM. This doesn't references the variable i
from your loop.
You can simply write the index as argument and use this directly in your function delete_book
.
var library = []; var title_input = document.getElementById("title"); var author_input = document.getElementById("author"); var date_input = document.getElementById("date"); var read_input = document.getElementById("read"); function Book(title, author, date, read) { this.title = title; this.author = author; this.date = date this.read = read }; function add_book() { var newBook = new Book(title_input, author_input, date_input, read_input) library.push(`Title: ${newBook.title.value} <br>`+`Author: ${newBook.author.value} <br>`+ `Realease date: ${newBook.date.value} <br>`+`Readed: ${newBook.read.value} <br>` ) show_library(); }; function delete_book(arr, index){ arr.splice(index, 1); show_library(); } function show_library() { document.getElementById("display").innerHTML = ""; for(i = 0; i<library.length; i++){ document.getElementById("display").innerHTML += library[i]+ `<button onclick="delete_book(library, ${i});">Delete</button><br>`; } };
<html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="./styles.css" /> <title>Document</title> </head> <body> <h1>My Library</h1> <input id="title" type="text" placeholder="Book Title"> <input id="author" type="text" placeholder="Book Author"> <input id="date" type="text" placeholder="Publish Date"> <select id="read" name="read"> <option value="yes">yes</option> <option value="no">no</option> </select> <input type="button" value="New Book" onclick="add_book()"> <div id="display"></div> <script src="app.js"></script> </body> </html>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.