Skip to content Skip to sidebar Skip to footer

How To Implement Local Storage On Html?

(Updated)Here is the View Source. For Example: You have a list of Names..I have to use a foreach loop because are over 100 names. And once the user selects the name, I have there

Solution 1:

Instead of doing it in the server side, use the client side to make it possible.

function updateSelection(which) {
  if (typeof localStorage != "undefined")
    localStorage.setItem("select", which.value);
}
window.onload = function () {
  if (typeof localStorage != "undefined")
    document.querySelector("#sel").value = localStorage["select"];
};
<select id="sel" onchange="updateSelection(this);">
  <option value="1">Option 1</option>
  <option value="2">Option 2</option>
  <option value="3">Option 3</option>
  <option value="4">Option 4</option>
  <option value="5">Option 5</option>
</select>

If the Stack Snippets are sandboxed, see the live preview at JSBin.


Post a Comment for "How To Implement Local Storage On Html?"