I'm trying to console.log a value taken from an input from session storage but it gives me " [object HTMLInputElement " but not the actual value of the input what can be done? Here's my code
let us = document.getElementById('us')
sessionStorage.setItem("user",us)
setTimeout(function() {
let h = sessionStorage.getItem("user")
console.log(h)
}, 200);
CodePudding user response:
Your script is working correctly but document.getElementById('us') is html element, you store HTML element. I think you need value or something, you write or change like this document.getElementById('us').value or what you need to do.
CodePudding user response:
As @Huseyint said, you must use <HTMLInputElement>.value
to get the value of the input field. More info here. For example, your code would be rewritten like this:
const us = document.getElementById('us');
sessionStorage.setItem('user', us.value);
const h = sessionStorage.getItem('user');
console.log(h);
Or if you want to save all of the properties you could do something like this:
const us = document.getElementById('us');
sessionStorage.setItem('user', us);
const h = sessionStorage.getItem('user').value;
console.log(h);
Either one should work. Also, note that setItem()
isn't a promise so you don't need the setTimeout
.
Hoped this helped!