I am extremely new to programming and an assignment from class was to create a voting website. I was able to create variables and put them into local storage, as such:
var eventName = document.getElementById("eventName").value;
document.getElementById("p1").innerHTML = (eventName);
localStorage.setItem("eventName", eventName)
localStorage.getItem("eventName")
Now, we were supposed to include all of this into an array so that we can get multiple eventNames. My teacher is never really clear with his instructions, so this is what I got now:
var eventName = [];
var index ;
function submitNewEvent() {
eventName[index] = document.getElementById("eventName").value;
index = index + 1;
var eventNmString = JSON.stringify (eventName);
localStorage.setItem("eventName", JSON.stringify (eventName));
localStorage.getItem("eventName")
array = JSON.parse(localStorage.getItem("eventName"));
array = parse;
var output = "";
for (var i=0, array.length > i; i++){
output += "<p>"+array[i];
}
document.getElementById("p1").innerHTML = (output);
I would really appreciate any help if anyone can explain to me what I did wrong.
array = parse;supposed to do? There is noparsevariable shown, so that should give an error.var index;should bevar index = 0;. Also, why are you usinglocalStorageat all? In the code shown the only time you retrieve the value with.getItem()is immediately after storing it with.setItem(), so that seems pointless - the idea withlocalStorageis to store items now and retrieve them in some later session.