It doesn’t have a value. You need listen for an input event, and then get the value when that happens. Because you aren’t waiting for that, it logs the value that’s there, which is nothing.
document.querySelector("form").addEventListener("submit",function(){
let name=document.getElementById("task").value;
let age=document.getElementById("task1").value;
let domain=document.getElementById("task2").value;
//console.log(name); //not working
var obj={
nobj:name,
aobj:age,
dobj:domain,
};
// localStorage.setItem("details",JSON.stringify(obj));
let details;
if(localStorage.getItem("details")===null){
details=[];
}else{
details=JSON.parse(localStorage.getItem("details"));
}
details.push(obj);
localStorage.setItem("details",JSON.stringify(details));
});
Can you explain the need of JSON.parse() and JSON.stringify() ? I tried before without giving stringify() and parse() as I had already created the empty array and inserted the first value in it. After that the value were not inserting into the array. please explain
What will be the value of details now ?
Why we are using parse when .parse() ?? What is the need of doing this when details is already an array? What if we would do