client.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // client-side js
  2. // run by the browser each time your view template is loaded
  3. console.log("hello world :o");
  4. // our default array of dreams
  5. const dreams = [
  6. "Find and count some sheep",
  7. "Climb a really tall mountain",
  8. "Wash the dishes"
  9. ];
  10. // define variables that reference elements on our page
  11. const dreamsList = document.getElementById("dreams");
  12. const dreamsForm = document.forms[0];
  13. const dreamInput = dreamsForm.elements["dream"];
  14. // a helper function that creates a list item for a given dream
  15. const appendNewDream = function(dream) {
  16. const newListItem = document.createElement("li");
  17. newListItem.innerHTML = dream;
  18. dreamsList.appendChild(newListItem);
  19. };
  20. // iterate through every dream and add it to our page
  21. dreams.forEach(function(dream) {
  22. appendNewDream(dream);
  23. });
  24. // listen for the form to be submitted and add a new dream when it is
  25. dreamsForm.onsubmit = function(event) {
  26. // stop our form submission from refreshing the page
  27. event.preventDefault();
  28. // get dream value and add it to the list
  29. dreams.push(dreamInput.value);
  30. appendNewDream(dreamInput.value);
  31. // reset form
  32. dreamInput.value = "";
  33. dreamInput.focus();
  34. };