Closed ghost closed 3 years ago
Week 4 Step 2 ⬤⬤◯◯◯◯◯ | 🕐 Estimated completion: 20-25 minutes
Ideally, users will fill the web form with the correct information. However, people make mistakes. In this step, we will be implementing some form validation to make sure that a user has inputted text value before submitting their password.
bunnimage-frontend
branch multipart/form-data
in index.html
index.html
that accepts image/x-png,image/jpeg
and add the attribute name=image
getImage
event handler function in index.js
to create an alert if either the file input or the name
input is nulltype
attribute to "submit"
bunnimage/index.html
and bunnimage/script.js
Open up your LiveServer plugin and start your server.
By default, the website will reload whenever a user presses submit. This leads to our text on the page that we set being cleared away. We can prevent this by adding this to the top of our getImage()
function:
event.preventDefault()
In HTML Forms, the enctype
attribute specifies how the form-data should be encoded when submitting it to the server. Like we learned before, if we want to upload files, we need send form-data encoded as multipart/form-data
<h1>Bunnimage</h1>
<form onsubmit="getImage(event)" enctype="multipart/form-data">
<label>Code: </label>
To add the image upload input, add an additional file input within the form & change the submit button's type
.
<form onsubmit="getImage(event)" enctype="multipart/form-data">
<input type="file" name="image"></input>
<input id="name" type="text" placeholder="Enter your file's name">
<input type="submit" />
We need to validate two things.
Hop on over to the next issue.
Week 4 Step 1 ⬤◯◯◯◯◯◯ | 🕐 Estimated completion: 20-25 minutes
Adding an Event Handler to your HTML Form!
Demo: 🐰
Congrats! If you made it this far, your Azure functions can now return data. However, users won't want to make POST requests with Postman to use your API though, so let's make it more user friendly.
✅ Task:
bunnimage-frontend
branchgetImage
event handler function when the submit button is clickedgetImage
event handler function inindex.js
to displayinput
appended with a ❤️ in theoutput
divbunnimage/index.html
andbunnimage/script.js
bunnimage-frontend
tomain
, but do not merge it🚧 Test your Work
Use LiveServer! This is a helpful VSCode extension that allows you to see your HTML that updates while you edit it.
:question: How do you use LiveServer?
![image](https://user-images.githubusercontent.com/69332964/99007366-0fd21f80-2512-11eb-9af9-311d89098c0b.png) * To start a local server, click `Go live` at the bottom right of the screen, as shown in the image. * Make sure that you have the entire repo open on VS Code and not just the individual files. * If this is your first time installing LiveServer, you might need to close/quit VS Code and reopen it. * Test it out, and see what your HTML page looks like! *It's OK if it's boring, so feel free to style it with CSS!*
Adding jQuery
jQuery takes a lot of common tasks that require many lines of JavaScript code to accomplish, and wraps them into methods that you can call with a single line of code. We are going to be using the
.text
method to change the text displayed on theoutput
div.index.js
inindex.html
:question: How do I import jQuery?
Put it **at the very end of the HTML page outside of all the tags!** ```html ```:exclamation: How can I connect my JS file to my HTML?
Great question! All we have to do is reference it just like we did with the jQuery. ```html ... ``` > Place this directly under your jQuery reference.Writing the Event Handler Function!
On websites, there are many different types of events that can occur. For example:
We need to create an event handler function called
getImage()
, which triggers when someone submits an image.We are going to do this by using the jQuery
text()
method. The syntax to set the text for a div is$(selector).text(content)
.To learn more, read the docs here
I'm confused on how to do this
- The selector should be `#output`, or the name of the div with a # in front. - The content should be `document.getElementById("username").value + "❤️"` > :bulb: We are retrieving the value of the "name" text box with this code! `$('#output').text(document.getElementById("username").value + "❤️")`How do I call the event handler from my HTML form?
The
onclick
event executes a Javascript function when a button is clicked