This lab will further help you understand the concepts surrounding Common Web APIs for Events.
Flatapets wants to improve its website to allow its admin users to upload images for pets that are available for adoption using a file chooser and use a drag-and-drop feature to drag and drop the pet images from a list of displayed pet images to display the details for the dragged pet image. You will need to use your knowledge of Common Web APIs for Events 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.
load event: An event that occurs once all contents of a file have loaded (in the case of a FileReader
object that is listening for the load
event).
dragstart event: An event that occurs when a user starts dragging an element or text selection.
dragover event: An event that occurs when an element or text selection is being dragged over a valid drop target.
drop event: An event that occurs when an element or text selection is dropped on a valid drop target.
Here are some other useful resources:
- MDN
In this lab, you will practice working with the File API to retrieve file data from a file chosen using a file chooser, and the HTML Drag and Drop API to add a drag-and-drop feature to a web page.
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-pet-form
that will allow the<form>
element to listen for thesubmit
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 (Hint: You can check the value of a parameter or variable usingconsole.log()
). 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. - The
File
object for the file chosen from the file<input>
element with the id ofnew-image
is stored into a variable namedfile
. - A new
FileReader
object is created and stored into a variable namedfileReader
. - An event listener is added to the
FileReader
object, referenced by thefileReader
variable, that will allow theFileReader
object to listen for aload
event and will call thehandleLoad()
function in response to theload
event. - The
readAsDataURL()
method is called on theFileReader
object, referenced by thefileReader
variable. TheFile
object, referenced by thefile
variable, is passed in as an argument to thereadAsDataURL()
method.
- The
handleLoad(event)
: ThehandleLoad()
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 aProgressEvent
object when the correct value is passed as an argument into the function (Hint: You can check the value of a parameter or variable usingconsole.log()
). When thehandleLoad()
function is called, the following actions should take place:- An
object
is created and stored into a variable namednewPet
. 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 theresult
key of theFileReader
object (Hint: You should be able to access theFileReader
object fromevent.target
, if theevent
parameter has the correct value). - 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
- The
addMovieToList()
function is called and theobject
referenced by thenewMovie
variable is passed in as an argument to theaddMovieToList()
function.
- An
addPetImageToPetsDiv(pet)
: TheaddPetImageToPetsDiv()
function has been declared for you and most of the code for this function has been written for you. It has 1 parameter namedpet
whose value is anobject
that contains the following keys:name
,image
, anddescription
. Write the code to add an event listener to the<img>
element, referenced by theimgElement
variable, that will allow the<img>
element to listen for adragstart
event. When the event handler (callback function) for the event listener is called, the value of thedraggedPet
variable is set to the value of thepet
parameter from theaddPetImageToPetsDiv()
function. ThedraggedPet
variable has been declared for you in global scope.- Add an event listener to the
<img>
element with the class ofdetail-image
that will allow the<img>
element to listen for thedragover
event. When the event handler (callback function) for the event listener is called, thepreventDefault()
method is called on theDragEvent
object to allow for thedrop
event to occur on the<img>
element. - Add an event listener to the
<img>
element with the class ofdetail-image
that will allow the<img>
element to listen for thedrop
event. When the event handler (callback function) for the event listener is called, thedisplayPetDetails()
function is called and theobject
referenced by thedraggedPet
variable is passed in as an argument to thedisplayPetDetails()
function.
When you’re done with this lab, remember to commit and push your changes to GitHub, then submit your work to Canvas using CodeGrade.