I have a javascript application where I need to save the user selections in a HTML form that I need to retrun to the form when the user returns to the page with the form. I have implemented solutions that I have found here on stackoverflow, in fact the code below that I am using comes from this link
Here are two versions of that code
function dataLoad()
{
if (typeof(Storage)!== "undefined")
{
if (localStorage.getItem('settingsObject') !== null)
{
console.log("in dataLoad");
var inputParse = JSON.parse(localStorage.getItem('settingsObject'));
console.log(inputParse);
$.each(inputParse, function (key, value) {
var field = document.getElementById(key);
if(field.type == 'radio' || field.type == 'checkbox'){
field.checked = value;
}else{
field.value = value;
}
});
}
}
else
{
document.getElementById("result").innerHTML = "Sorry, your browser does not support Web Storage...";
}
}
function dataLoad()
{
if (typeof(Storage) !== "undefined")
{
if (localStorage.getItem('settingsObject') !== null)
{
alert("in dataLoad");
var inputParse = JSON.parse(localStorage.getItem('settingsObject'));
console.log(inputParse);
$.each(inputParse, function(key, value) {
var field = document.querySelector("[name=" + key + "]");
if (field.type == 'radio' || field.type == 'checkbox') {
field.checked = field.value == value;
} else {
field.value = value;
}
});
}
}
else
{
document.getElementById("result").innerHTML = "Sorry, your browser does not support Web Storage...";
}
}
Here is my form
<form id="settingsInput" class="dropdown contents" autocomplete="on" target="hiddeniFrame">
<fieldset class="locfield">
<legend>Location<br></legend>
<input type="radio" name="location" value="auto" id="autoloc" checked><label for="autoloc"> Auto</label><br><hr class="settingsDivider">
<input type="radio" name="location" value="city" id="cityloc"><label for="cityloc"> City</label><br>
Select City:<br>
<select name="cities" class="locinput" style="width:6em">
<option value="0" selected>U.S. Cities</option>
<option value="1">Albuquerque, NM</option>
<option value="2">Anchorage, AK</option>
<option value="3">Atlanta, GA</option>
<option value="4">Austin, TX</option>
</select><br>
<span><label for="Citydst" style="font-size:15px">Daylight Saving?</label></span><br>
<input type="checkbox" name="CityDST" value="DST" id="Citydst"><br>
<hr class="settingsDivider">
<input type="radio" name="location" value="manual" id="manualloc"><label for="manualloc"> Manual</label><br>
Latitude:<br> <input type="number" name="latitude" maxlength="10" class="locinput" style="width:6em"><br>
<input type="radio" name="NS" value="north" id="North"><label for="North"> North</label><br>
<input type="radio" name="NS" value="south" id="South"><label for="South"> South</label><br>
Longitude:<br> <input type="number" name="longitude" class="locinput" style="width:6em"><br>
<input type="radio" name="EW" value="east" id="East"><label for="East"> East</label><br>
<input type="radio" name="EW" value="west" id="West"><label for="West"> West</label><br>
Elevation:<br> <input type="number" name="landelev" class="locinput" style="width:6em"><br>
<input type="radio" name="elevunits" value="feet" id="feet"><label for="feet"> Feet</label><br>
<input type="radio" name="elevunits" value="meters" id="meters"><label for="meters"> Meters</label><br>
Time Zone:<br> <select name="timezones">
<option value="-12">UTC-12</option>
<option value="-11">UTC-11</option>
<option value="-10">UTC-10</option>
<option value="-9">UTC-9</option>
<option value="-8">UTC-8</option>
<option value="-7">UTC-7</option>
<option value="-6">UTC-6</option>
<option value="-5">UTC-5</option>
<option value="-4">UTC-4</option>
<option value="-3">UTC-3</option>
<option value="-2">UTC-2</option>
<option value="-1">UTC-1</option>
<option value="0">UTC+0</option>
<option value="1">UTC+1</option>
<option value="2">UTC+2</option>
<option value="3">UTC+3</option>
<option value="4">UTC+4</option>
<option value="5">UTC+5</option>
<option value="6">UTC+6</option>
<option value="7">UTC+7</option>
<option value="8">UTC+8</option>
<option value="9">UTC+9</option>
<option value="10">UTC+10</option>
<option value="11">UTC+11</option>
<option value="12">UTC+12</option>
</select><br>
<span><label for="Manualdst" style="font-size:15px">Daylight Saving?</label></span><br>
<input type="checkbox" name="ManualDST" value="DST" id="Manualdst">
</fieldset>
<fieldset class="datefield">
<legend>Date</legend>
<input type="radio" name="dateMode" value="auto" id="autodate" checked><label for="autodate"> Auto</label><br><hr class="settingsDivider">
<input type="radio" name="dateMode" value="manual" id="manualdate"><label for="manualdate"> Manual</label><br>
Select Date:<br>
<input type="date" name="dateInput" class="dateinput" style="width:6em"><br><hr class="settingsDivider">
<!--<label for="dst" style="font-size:15px">Daylight Saving?</label></span><br>
<input type="checkbox" name="DST" value="DST" id="dst"><span style="font-size: 13px">-->
</fieldset>
<fieldset class="prayerfield">
<legend>Prayers</legend>
<!--<span style="text-decoration: underline">Subh Cond:</span><br>-->
<span>Subh Cond:</span><br>
<input type="radio" name="subhMode" value="12" id="subhurban" checked><span><label for="subhurban"> Urban</label></span><br>
<input type="radio" name="subhMode" value="15" id="subhwild"><span style="font-size: 17px"><label for="subhwild"> Wilderness</label></span><br><hr class="settingsDivider">
<span class="dhuhurtext">Dhuhur Shadow:</span><br>
<input type="radio" name="dhuhurMode" value="42" id="dhuhursandal"><span style="font-size: 15px"><label for="dhuhursandal"> Sandal Strap</label></span><br>
<input type="radio" name="dhuhurMode" value="21" id="dhuhurhand" checked><span style="font-size: 17px"><label for="dhuhurhand"> Hand Span</label></span><br>
<input type="radio" name="dhuhurMode" value="7" id="dhuhurfoot"><span style="font-size: 17px"><label for="dhuhurfoot"> Foot Span</label></span><br><hr class="settingsDivider">
<!--<span style="text-decoration: underline">Asr Shadow:</span><br>-->
<span>Asr Shadow:</span><br>
<input type="radio" name="asrMode" value="1" id="asrmajority" checked><span style="font-size: 20px"><label for="asrmajority"> Majority</label></span><br>
<input type="radio" name="asrMode" value="2" id="asrhanafi"><span><label for="asrhanafi"> Hanafi</label></span><br><hr class="settingsDivider">
<!--<span style="text-decoration: underline">Isha Cond:</span><br>-->
<span>Isha Cond:</span><br>
<input type="radio" name="ishaMode" value="12" id="ishaurban"><span><label for="ishaurban"> Urban</label></span><br>
<input type="radio" name="ishaMode" value="15" id="ishawild" checked><span style="font-size: 17px"><label for="ishawild"> Wilderness</label></span><br>
</fieldset>
<div class="set-section">
<button type="button" name="prayertimes" value=" Save " onclick="chooseMethod(this.form)">Save</button>
</div>
<div class="update-section">
<button type="button" name="updateprayertimes" value=" Update " onclick="chooseMethod(this.form)">Update</button>
</div>
<div class="reset-section">
<button type="reset" value=" Reset ">Reset</button>
</div>
<script>
document.getElementById("settingsInput").addEventListener("submit", function(e) {
e.preventDefault();
});
//Rest of your js code here...
</script>
</form>
I have had more success with the bottom version than the top version. Actually the top version does not seem to repopulate any of the elements, while the bottom version does a better job, but fails to populate the radio buttons named “location” and the radio buttons named “auto”, “cities”, and “manual”, as well as the radio buttons named “NS” and “EW”. Sometimes the radio button for “dateMode” doe not get repopulated. The funny thing is that the radio buttons named “subhMode”, “dhuhurMode”, “asrMode”, and “ishaMode” always get repopulated.
What am I doing wrong?