-
Notifications
You must be signed in to change notification settings - Fork 0
/
add-form 2.js
39 lines (28 loc) · 963 Bytes
/
add-form 2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// ------------DOM Elements------------------------- //
const addForm = document.querySelector("#add-book-form")
// ------------Fetch Functions------------------------- //
function addBookFetch(bookObj) {
fetch("http://localhost:3000/books", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(bookObj)
})
}
// ------------Event Functions------------------------- //
function handleAdd(event) {
event.preventDefault()
const formField = event.target
const newBook = {
title: formField.title.value,
author: formField.author.value,
genre: formField.genre.value,
image_url: formField.image_url.value,
year: formField.year.value,
description: formField.description.value
}
addBookFetch(newBook)
}
// ------------Event Listener------------------------- //
addForm.addEventListener("submit", handleAdd)