The assignment is to create a form, alert the user if they have not filled in all the fields, then assign each field into an individual variable and create an object from it. Then, prompt the user to enter a number from 2-10 and a word and display the word as many times as the number that was input, along with the information that was input by the user.
After the user has input everything, and submits the final prompt, the field just resets to the beginning.
My understanding is using form action="#" prevents it from actually being submitted, and instead does the actions within the functions.
I would appreciate any help.
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="css/form.css">
<title>Form</title>
</head>
<body>
<form action="#" method="post" id="my-form">
<fieldset>
<label for="fname">First Name</label>
<input type="text" id="fname" name="fname">
<label for="lname">Last Name</label>
<input type="text" id="lname" name="lname">
<label for="age">Age</label>
<input type="text" id="age" name="age">
<label for="language">Language</label>
<select id="language" name="language">
<option value="english">English</option>
<option value="spanish">Spanish</option>
<option value="french">French</option>
<option value="mandarin">Mandarin</option>
</select>
<label for="email">Enter your email:</label>
<input type="email" id="email" name="email">
<input type="submit" id="submit-btn" value="Submit">
</fieldset>
</form>
<div id="myDiv" name="myDiv"></div>
<script src="js/midterm.js"></script>
</body>
</html>
let person;
function getInput() {
'use strict';
let fname = document.getElementById('fname').value;
let lname = document.getElementById('lname').value;
let age = document.getElementById('age').value;
let email = document.getElementById('email').value;
if(fname == "" || lname == "" || age == ""|| email == "")
{
alert("Please fill in all fields.");
}
else{
person = {
"fname": fname,
"lname": lname,
"age": age,
"email": email,
};
}
return false;
};
function displayUserInfo() {
'use strict';
let input;
let number;
let chosenWord;
do {
input = prompt("Please enter a number between 2-10");
}
while (input < 2 || input > 10);
number = input.value;
inputWord = prompt("Please enter any word of your choosing");
chosenWord = inputWord.value;
document.getElementById('myDiv').innerHTML += person.fname + " " + person.lname +
" " + person.email + " " + person.age + " " + person.language;
for(let x = 0; x < number; x++)
{
document.getElementById('myDiv').innerHTML +=
inputWord + "-" + x + " ";
}
};
function init() {
'use strict';
document.getElementById('submit-btn').onclick = getInput;
document.getElementById('my-form').onsubmit = displayUserInfo;
}
window.onload = init;
2
Answers
You might have to add a call to
event.preventDefault()
in your form submit handler. In this case, you will also have to pass the submit event as an argument to the function, like so:This stops the form from performing the default actions (such as refreshing the page) when it is submitted.
If you do this, then I believe you will actually not need to set the
action
attribute as it will be redundant (the action will never be carried out), so you can leave it out altogether along with themethod
attribute from the<form>
element.You can add the
required
attribute to the input fields to ensurethat the user fills in all the required fields. That will give an
alert on the field that it should be
required
.Use the
getInput
function to validate user input before proceeding. If one of the field is empty it will returnfalse
.You need to
addEventListener
to handle the form submission andprevent the default form submission behavior using
event.preventDefault()
method OR calldisplayUserInfo(evt) { evt.preventDefault(); }
in your form submit handler as mentioned by @MagicalCornFlake.You have also missed to add Language field value.
I have made changes to your code according to your need: