This practice lesson will help you understand the concepts surrounding Common Web APIs for Events.
Flatdango wants to improve its application to allow its admin users to upload images for new movies playing at the Flatiron Movie Theater using a file chooser and it also wants to use a drag-and-drop feature to drag and drop the name of a movie from a list of displayed movie names to display the details for the dragged movie name.
It is recommended that you use the Visual Studio Code (VSCode) IDE for this practice lesson.
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
Fork and clone this practice lesson 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-movie-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 first file chosen from the file<input>
element with the id ofposter-input
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 namednewMovie
. Theobject
contains the following key and value pairs:- A key of
title
whose value is set to the value of thevalue
attribute of the<input>
element with the id oftitle-input
. - A key of
runtime
whose value is set to the value of thevalue
attribute of the<input>
element with the id ofruntime-input
. - A key of
showtime
whose value is set to the value of thevalue
attribute of the<input>
element with the id ofshowtime-input
. - A key of
description
whose value is set to the value of thevalue
attribute of the<textarea>
element with the id ofdescription-textarea
. - A key of
poster
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
- The
addMovieToList()
function is called and theobject
referenced by thenewMovie
variable is passed in as an argument to theaddMovieToList()
function.
- An
addMovieToList(movie)
: TheaddMovieToList()
function has been declared for you and most of the code for this function has been written for you. It has 1 parameter namedmovie
whose value is anobject
that contains the following keys:title
,runtime
,showtime
,description
, andposter
. Write the code to add the following functionality to theaddMovieToList()
function:- Set the value of the
draggable
attribute of the<li>
element, referenced by theliElement
variable, totrue
. - Add an event listener to the
<li>
element, referenced by theliElement
variable, that will allow the<li>
element to listen for adragstart
event. When the event handler (callback function) for the event listener is called, the value of thedraggedMovie
variable is set to the value of themovie
parameter from theaddMovieToList()
function. ThedraggedMovie
variable has been declared for you in global scope.
- Set the value of the
- Add an event listener to the
<div>
element with the id oftitle
that will allow the<div>
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<div>
element. - Add an event listener to the
<div>
element with the id oftitle
that will allow the<div>
element to listen for thedrop
event. When the event handler (callback function) for the event listener is called, thedisplayMovieDetails()
function is called and theobject
referenced by thedraggedMovie
variable is passed in as an argument to thedisplayMovieDetails()
function.
function addMovieToList(movie){
const liElement = document.createElement('li');
liElement.className = "film item";
liElement.textContent = movie.title;
const filmsListElement = document.getElementById('films');
filmsListElement.appendChild(liElement);
// Task # 4 solution code
liElement.draggable = 'true';
liElement.addEventListener('dragstart', () => {
draggedMovie = movie;
});
}
// Task # 2 solution code
function handleSubmit(event){
event.preventDefault();
const posterInput = document.getElementById('poster-input');
const file = posterInput.files['0'];
const fileReader = new FileReader();
fileReader.addEventListener('load', handleLoad);
fileReader.readAsDataURL(file);
}
// Task # 3 solution code
function handleLoad(event){
const titleInput = document.getElementById('title-input');
const runtimeInput = document.getElementById('runtime-input');
const showtimeInput = document.getElementById('showtime-input');
const descriptionTextarea = document.getElementById('description-textarea');
const newMovie = {
title: titleInput.value,
runtime: runtimeInput.value,
showtime: showtimeInput.value,
description: descriptionTextarea.value,
poster: event.target.result
};
addMovieToList(newMovie);
}
// Task # 1 solution code
const newMovieForm = document.getElementById('new-movie-form');
newMovieForm.addEventListener('submit', handleSubmit);
// Task # 5 solution code
const titleDiv = document.getElementById('title');
titleDiv.addEventListener('dragover', (event) => {
event.preventDefault();
});
// Task # 6 solution code
titleDiv.addEventListener('drop', () => {
displayMovieDetails(draggedMovie);
});