Hi I need some help to add new objects to local storage every time I when I fill out a form and click on submit, I want to add new item to local storage is it possible? please advice and or help with my code below. thanks for the help. Everytime when I select from a dropdown value.
My dropdown form:
<form asp-action="Create">
<div asp-validation-summary="All" class="text-danger"></div>
<div class="form-group">
<label asp-for="ScheduleStartTime" class="control-label"></label>
<input asp-for="ScheduleStartTime" class="form-control" id="scheduleStartTime" />
<span asp-validation-for="ScheduleStartTime" class="text-danger"></span>
</div>
<select asp-for="CandidateID" class="form-control" id="candId">
@{
if (candidates != null)
{
foreach (var cand in candidates)
{
<option value='@cand.ID'>@cand.DisplayName</option>
}
}
}
</select>
<div class="form-group">
<input onclick="addTheEvent(); return false;" type="submit" value="Add " class="btn btn-primary" />
</div>
</form>
here is my code:
<script>
var addToTheContent = document.getElementById("canvas");
var scheduleEvent = document.getElementById("scheduleStartTime");
var candidateId = document.getElementById('candId');
var getCandId = document.getElementById("candId");
var displayCandId = getCandId.options[getCandId.selectedIndex].value;
function addTheEvent() {
var showText = addToTheContent.innerHTML = displayCandId + " ( " + scheduleEvent.value + " ) ";
localStorage.setItem("key", showText)
localStorage.getItem("key");
}
</script>
Updated:
function addTheEvent() {
var showText = addToTheContent.innerHTML = displayCandId + " (" + scheduleEvent.value + ") ";
//set value into local storage
localStorage.setItem("key", JSON.stringify(showText));
localStorage.getItem('key');
// localStorage.getItem("key") // get value from localStorage
window.location.href = "/";
}