Created
March 9, 2018 17:53
-
-
Save Julian-Nash/4500dee09abdf57cbaea4a5a5af1f9f1 to your computer and use it in GitHub Desktop.
Save and populate a form with sessionStorage data from a previously submitted form using jQuery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Get the previous form data from sessionStorage | |
var previousForm = JSON.parse(sessionStorage.getItem("formData")); | |
// Iterate through the form fields and polulate the form with sessionStorage values | |
for (let item in previousForm) { | |
$(`#${item}`).val(`${previousForm[item]}`); | |
} | |
$("#submit").click(function(){ | |
// Build sessionStorage object on form submit click | |
var formData = { | |
name: $("#name").val(), | |
company_number: $("#company_number").val(), | |
vat_number: $("#vat_number").val(), | |
contact: $("#contact").val(), | |
email: $("#email").val(), | |
address: $("#address").val(), | |
postcode: $("#postcode").val(), | |
country: $("#country").val(), | |
description: $("#description").val(), | |
amount: parseFloat($("#amount").val()), | |
}; | |
// Save the object to sessionStorage | |
sessionStorage.setItem("formData", JSON.stringify(form_data)); | |
// Do something else here - Send an ajax post request etc. | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment