sessionStorage on DOMContentLoaded javascript 1

sessionStorage on DOMContentLoaded javascript

nameInput.addEventListener("input", () => {
  sessionStorage.setItem("name", nameInput.value);
});

emailInput.addEventListener("input", () => {
  sessionStorage.setItem("email", emailInput.value);
});

msgInput.addEventListener("input", () => {
  sessionStorage.setItem("message", msgInput.value);
});

document.addEventListener("DOMContentLoaded", () => {
  nameInput.value = sessionStorage.getItem("name");
  emailInput.value = sessionStorage.getItem("email");
  msgInput.value = sessionStorage.getItem("message");
});

Here is what the above code is Doing:
1. We’re adding an event listener to each input field.
2. When the input field is changed, we’re storing the value of the input field in sessionStorage.
3. When the DOM is loaded, we’re setting the value of the input field to the value stored in sessionStorage.

Similar Posts