This lab will further help you understand the concepts surrounding HTML Forms.
The Flatburger restaurant wants to add functionality to their website that displays a menu of food to sell to its customers to allow users to add a new food to their website. You will need to use your knowledge of HTML Forms to complete this lab.
It is recommended that you use the Visual Studio Code (VSCode) IDE for this lab.
Useful considerations and terminology:
HTML Form: A form for user input that is created by a <form>
element that consists of opening and closing <form>
tags that enclose one or more <input>
elements where users can enter information, and a way to submit the form.
Event: Something a user does on a web page or something that happens on a web page.
Event handler: A callback function that executes code in response to an event.
submit event: An event that occurs when a user clicks on a <button>
element or an <input>
element (whose type
attribute is set to submit
) within a <form>
to submit a form on a web page.
Here are some other useful resources:
In this lab, you will practice adding an event listener to allow for a <form>
element to listen for the submit
event and execute code in response to the submit
event.
Fork and clone this lab into your local environment. Navigate into its
directory in the terminal, then run code .
to open the files in Visual Studio
Code.
Then, open the index.html
file on your browser to run the application.
Write your code in the index.js
file. There is some starter code provided in index.js
.
These are your tasks:
- Add an event listener to the
<form>
element with the id ofnew-food-form
that will allow the<form>
element to listen for asubmit
event and will call thehandleSubmit()
function in response to thesubmit
event. handleSubmit(event)
: ThehandleSubmit()
function has been declared for you, but you will need to write the code that should go inside of this function. It has 1 parameter namedevent
whose value should be aSubmitEvent
object, when the correct value is passed as an argument into the function. When thehandleSubmit()
function is called, the following actions should take place:- The
preventDefault()
method is called on theSubmitEvent
object to prevent the page from refreshing. - An
object
is created and stored into a variable namednewFood
. Theobject
contains the following key and value pairs:- A key of
name
whose value is set to the value of thevalue
attribute of the<input>
element with the id ofnew-name
. - A key of
image
whose value is set to the value of thevalue
attribute of the<input>
element with the id ofnew-image
. - A key of
description
whose value is set to the value of thevalue
attribute of the<textarea>
element with the id ofnew-description
. - A key of
healthy
whose value is set totrue
if the value of thevalue
attribute of the<select>
element with the id ofhealthy-select
is strictly equal tohealthy
, andfalse
otherwise.
- A key of
- The
object
referenced by thenewFood
variable is added to the array referenced by thefoods
variable. If
the value of thevalue
attribute of the<select>
element with the id offood-select
is strictly equal toall
, theaddFoodImageToMenu()
function is called and theobject
referenced by thenewFood
variable is passed in as an argument to theaddFoodImageToMenu()
function.If
the value of thevalue
attribute of the<select>
element with the id offood-select
is strictly equal tohealthy
, theaddHealthyFoodToMenu()
function is called and theobject
referenced by thenewFood
variable is passed in as an argument to theaddHealthyFoodToMenu()
function.
- The
When you're done with this lab, remember to commit and push your changes up to GitHub, then submit your work to Canvas using CodeGrade.