I'm trying to store oninput event listener value in the local storage of the web browser. For example: if I'm typing something in a form input field it should be stored in local storage and when I refresh the page it should still be there. I read several blogs and watched tutorials on local storage and did the same but it isn't working for me. Can anyone please point out the mistake and how can I fix that?
Thanks in Advance!
<form class="form" action="https://formspree.io/f/meqvlgqr" method="POST">
<input
type="text"
name="name"
maxlength="30"
placeholder="Full Name"
required
class="input"
/>
<input
type="email"
name="email"
placeholder="example@gmail.com"
required
class="input"
/>
<textarea
rows="4"
cols="50"
name="message"
placeholder="Write your message here ..."
required
maxlength="500"
class="input-textarea"
></textarea>
<button type="submit" class="contact-button">Get It Touch</button>
</form>
oninput value is being displayed on inspector tool in console tab but when I go to application tab and check storage it displayed the JSON object but its values are empty.
const storeName = document.querySelector('input[type=text]');
const storeEmail = document.querySelector('input[type=email]');
const storeMessage = document.querySelector('.input-textarea');
const userDetails = { name: '', email: '', message: '' };
storeName.addEventListener('input', (e) => {
userDetails.name = e.target.value;
console.log(userDetails.name);
});
localStorage.setItem('userDetails', JSON.stringify(userDetails));