-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
93 lines (71 loc) · 3.03 KB
/
script.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
(function(){
"use strict";
const detailsForm = document.querySelector("#destination_details_form");
detailsForm.addEventListener("submit", handleFormSubmit);
// When someone submits the form, this function will run.
function handleFormSubmit(event){
event.preventDefault();
// Read out the inputs
const destName = event.target.elements["name"].value;
const destLocation = event.target.elements["location"].value;
const destPhoto = event.target.elements["photo"].value;
const destDesc = event.target.elements["description"].value;
// Clear all the fields
for(let i=0; i<detailsForm.length; i++){
detailsForm.elements[i].value = "";
}
// Run a function to create a new card
const destCard = createDestinationCard(destName, destLocation, destPhoto, destDesc);
const wishListContainer = document.getElementById("destination_container");
// Change the text in the heading
if(wishListContainer.children.length === 0){
document.getElementById("title").innerHTML = "My Wish List";
}
// Add the card
wishListContainer.appendChild(destCard);
}
function createDestinationCard(name, location, photoURL, description){
const card = document.createElement("div");
card.className = "card";
const img = document.createElement("img");
img.setAttribute("alt", name);
const constantPhotoUrl = "images/signpost.jpg";
if(photoURL.length === 0){
img.setAttribute("src", constantPhotoUrl);
}
else {
img.setAttribute("src", photoURL);
}
card.appendChild(img);
const cardBody = document.createElement("div");
cardBody.className = "card-body";
const cardTitle = document.createElement("h3");
cardTitle.innerText = name;
cardBody.appendChild(cardTitle);
const cardSubtitle = document.createElement("h4");
cardSubtitle.innerText = location;
cardBody.appendChild(cardSubtitle);
if(description.length !== 0){
const cardText = document.createElement("p");
cardText.className = "card-text";
cardText.innerText = description;
cardBody.appendChild(cardText);
}
const removeButton = document.createElement("button");
removeButton.innerText = "Remove";
removeButton.addEventListener("click", removeDestination);
cardBody.appendChild(removeButton);
card.appendChild(cardBody);
return card
}
function removeDestination(event){
// Remove a card
const card = event.target.parentElement.parentElement;
card.remove();
// Change the text in the heading if necessary
const wishListContainer = document.getElementById("destination_container");
if(wishListContainer.children.length === 0){
document.getElementById("title").innerHTML = "Enter Destination Details";
}
}
})();