If the answer is correct, i set in local storage a string object data.color
. Then each time the answer is correct i want to keep a log of that in the local storage and print the result.
Perhaps something is wrong with my logic and the text is not appending properly when page is reloaded, only the last result is outputted to the div. I want to append every result in the local storage when answer is correct.
$('#butn').click(function(e) {
$.ajax({
type: "POST",
url: "validation.php",
dataType: "json",
data: $("input").serialize(),
}).done(function(data) {
if (data.answer) { //if answer is correct
localStorage.setItem("ColorStorage", data.color); //set in local storage the string object
$("#result").append(localStorage.getItem("ColorStorage"));//print result to html
}
});
});
if (localStorage.getItem("ColorStorage")) {/*if local storage is set..*/
$("#result").append(localStorage.getItem("ColorStorage"));
}
html:
<div id="result"></div>