Error: [Object Htmlinputelement]

Asked

Viewed 9,360 times

0

How do I resolve the error: [Object Htmlinputelement]

The code should save the text typed in the field, and even giving F5 stay saved, example: same as the script of this site.

function digGet(){
    // Cria um item "usuario" com valor "Thiago Belem"
    var login = document.getElementById("user_login");
    window.localStorage.setItem('usuario', login);
    saveGet();
}

function saveGet(){
    // Depois, em outra página ou aba, recupera esse item
    var usuario = window.localStorage.getItem('usuario');
    document.getElementById('user_login').value = usuario;
}

function delGet(){

    // Remove o item
    window.localStorage.removeItem('usuario');
}

//Rodar no load do DOM
document.addEventListener("DOMContentLoaded", function(event){ 
    saveGet();
});
  • Please be clearer on your problem, as not even the full error message you put up. And do this editing the question, not in the comments. Start by describing what your code should do and what’s going on. Also put the full error message and work out a [mcve].

  • The variable login is a gift element in the digGet method, I believe you should write the value.

  • Hello Jeferson, there are a lot of people around here who know JS :) Looking at your code .setItem('usuario', login) I see you’re trying to keep an element of the DOM in localStorage that keeps only strings. That’s not going to work. I think what you want is to use .value or innerHTML... guy: var login = document.getElementById("user_login").value;

1 answer

3


In this section, the value

function digGet(){
    //Correcao aqui
    //Antes login era um elemento do Dom e nao o valor do elemento
    var login = document.getElementById("user_login").value;
    
    
    
    window.localStorage.setItem('usuario', login);
    saveGet();
}

  • thanks friend, manage to solve the problem of recording and.e

  • @Jefersonsantos blz mano

Browser other questions tagged

You are not signed in. Login or sign up in order to post.