var myFunction = function() {
if (typeof(Storage) !== "undefined") {
var e = this.value;
var current_value = localStorage.getItem("check");
if (current_value == "null") {
current_value = e;
} else {
current_value = current_value + "\n" + e;
}
localStorage.setItem("check", current_value);
console.log(localStorage.getItem("check"));
} else {
console.log('Cannot access local storage.');
}
};
var classname = document.getElementsByClassName("click_button")
for (var i = 0; i < classname.length; i++) {
classname[i].addEventListener("click", myFunction);
}
function clickCount() {
if (typeof(Storage) !== "undefined") {
alert(localStorage.getItem("check"));
} else {
alert("Cannot access local Storage");
}
}
<button type="button" id="first_button" class="click_button">first_button</button>
<button type="button" id="second_button" class="click_button">second_button</button>
<button type="button" id="third_button" class="click_button">third_button</button>
<button onclick="clickCount()" type="button" id="history_button">History_button</button>
When i click the history_button button, I have to alert the latest THREE buttons that were clicked(among first_button, second_button and third_button). This has to be done using HTML5 localStorage API. In my solution which I did anyway(Iam not a very good coder so my who code maybe wrong), alert shows all the previous clicks instead of only last three clicks. i.Please Help.