1. In the file script2.js, first change the line in the handleSubmit function which calls createCookies to instead call a function called saveSignUp.
1. In the file script2.js, first change the line in the handleSubmit function which calls createCookies to instead call a
function called saveSignUp.
3. At the top of the file, create a new global object variable “signUp” to store the sign-up information.
4. Add the following properties to the object: name, email, phone, address, city, state, zip, foodAllergies,
frequency, size and initialize all to a blank string
6. Define a new local array variable called “fields” in the populateInfo function.;
9. Immediately after the for loop, assign the properties of the signUp object to the values of the fields array you
just captured from the URL string.
10. Create a new function called saveSignUp().
11. Write a line of code to save the value of the textarea in the signUp object field for allergies.
scripts2.js
"use strict";
var queryArray = [];
function populateInfo() {
if (location.search) {
var queryData = location.search;
var hiddenInputs = document.querySelectorAll("input[type=hidden]");
queryData = queryData.substring(1, queryData.length);
queryArray = queryData.split("&");
for (var i = 0; i < queryArray.length; i++) {
hiddenInputs[i].value = queryArray[i].substring(queryArray[i].lastIndexOf("=") + 1);
}
}
}
function createCookies() {
var formFields = document.querySelectorAll("input[type=hidden],input[type=radio], textarea");
var expireDate = new Date();
expiresDate.setDate(expiresDate.getDate() + 7);
for (var i = 0; i < formFields.length; i++) {
var currentValue = decodeURIComponent(formFields[i].value);
currentValue = currentValue.replace(/\+/g, " ");
document.cookie = formFields[i].name + "=" + currentValue + "; expires=" + expiresDate.toUTCString();
}
}
function handleSubmit(evt) {
if (evt.preventDefault) {
evt.preventDefault();
} else {
evt.returnValue = false;
}
createCookies();
document.getElementsByTagName("form")[0].submit();
}
function createEventListeners() {
var form = document.getElementsByTagName("form")[0];
if (form.addEventListener) {
form.addEventListener("submit", handleSubmit, false);
} else if (form.attachEvent) {
form.attachEvent("onsubmit", handleSubmit);
}
}
function setupPage() {
createEventListeners();
populateInfo();
}
if (window.addEventListener) {
window.addEventListener("load", setupPage, false);
} else if (window.attachEvent) {
window.attachEvent("onload", setupPage);
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images