2019-10-07 14:26:35 +00:00
|
|
|
// client-side js
|
2018-03-13 20:25:39 +00:00
|
|
|
// run by the browser each time your view template is loaded
|
|
|
|
|
2019-09-30 21:05:06 +00:00
|
|
|
console.log("hello world :o");
|
2018-06-25 18:47:57 +00:00
|
|
|
|
|
|
|
// our default array of dreams
|
|
|
|
const dreams = [
|
2019-09-30 21:05:06 +00:00
|
|
|
"Find and count some sheep",
|
|
|
|
"Climb a really tall mountain",
|
|
|
|
"Wash the dishes"
|
2018-06-25 18:47:57 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
// define variables that reference elements on our page
|
2019-09-30 21:05:06 +00:00
|
|
|
const dreamsList = document.getElementById("dreams");
|
2018-06-25 18:47:57 +00:00
|
|
|
const dreamsForm = document.forms[0];
|
2019-09-30 21:05:06 +00:00
|
|
|
const dreamInput = dreamsForm.elements["dream"];
|
2018-06-25 18:47:57 +00:00
|
|
|
|
|
|
|
// a helper function that creates a list item for a given dream
|
|
|
|
const appendNewDream = function(dream) {
|
2019-09-30 21:05:06 +00:00
|
|
|
const newListItem = document.createElement("li");
|
2018-06-25 18:47:57 +00:00
|
|
|
newListItem.innerHTML = dream;
|
|
|
|
dreamsList.appendChild(newListItem);
|
2019-09-30 21:05:06 +00:00
|
|
|
};
|
2018-06-25 18:47:57 +00:00
|
|
|
|
|
|
|
// iterate through every dream and add it to our page
|
2019-09-30 21:05:06 +00:00
|
|
|
dreams.forEach(function(dream) {
|
2018-06-25 18:47:57 +00:00
|
|
|
appendNewDream(dream);
|
|
|
|
});
|
|
|
|
|
|
|
|
// listen for the form to be submitted and add a new dream when it is
|
|
|
|
dreamsForm.onsubmit = function(event) {
|
|
|
|
// stop our form submission from refreshing the page
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
// get dream value and add it to the list
|
|
|
|
dreams.push(dreamInput.value);
|
|
|
|
appendNewDream(dreamInput.value);
|
|
|
|
|
2019-09-30 21:05:06 +00:00
|
|
|
// reset form
|
|
|
|
dreamInput.value = "";
|
2018-06-25 18:47:57 +00:00
|
|
|
dreamInput.focus();
|
|
|
|
};
|