add remove functionality to my booklist pg

master
Adam Undus 2020-11-05 13:05:52 -05:00
parent b4f6630d0d
commit d7ea294ab9
1 changed files with 19 additions and 7 deletions

View File

@ -20,18 +20,30 @@ const displayUsingApi = false;
const body = $('#book-list') const body = $('#book-list')
Object.keys(list).forEach((bookId) => { Object.keys(list).forEach((bookId) => {
console.log(bookId) body.append(makeListItem(bookId, list[bookId]))
body.append(makeListItem(list[bookId]))
}) })
} }
function makeListItem(book) { $('.remove-from-booklist').on('click', (event) => {
const target = $(event.target)
const bookId = target.parent().attr('id')
console.log(bookId)
removeBook(bookId)
location.reload()
})
function removeBook (id) {
const newlist = JSON.parse(localStorage.getItem('booklist'))
delete newlist[id]
localStorage.setItem('booklist', JSON.stringify(newlist))
}
function makeListItem (id, book) {
const a = `<a href="${book.url}"><h3>${book.title}</h3></a>` const a = `<a href="${book.url}"><h3>${book.title}</h3></a>`
const li = `<li><div class="list-item"><img src="${book.imgSrc}"/>${a}<span>Author: ${book.author}</span><div><div class="social-button booklist"> const li = `<li><div id="${id}" class="list-item"><img src="${book.imgSrc}"/>${a}<span>Author: ${book.author}</span>
<button title="Add book to My Book List" id="add-to-booklist" <button title="Remove From Booklist" class="remove-from-booklist"
style="background-color: cornflowerblue;">+</button> style="background-color: indianred;">-</button>
</div></li>` </div></li>`
console.log(li)
return li return li
} }
})(jQuery) // eslint-disable-line })(jQuery) // eslint-disable-line