Skip to content

Instantly share code, notes, and snippets.

@Julian-Nash
Created March 9, 2018 17:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Julian-Nash/4500dee09abdf57cbaea4a5a5af1f9f1 to your computer and use it in GitHub Desktop.
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
// 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