0
I’m trying to get the content of an input field in html:
<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<input type="text" name="tarefa" placeholder="Insira sua tarefa">
<button onclick="Value1()">Pegar dado</button>
<button onclick="limpar()">Limpar localStorage</button>
<button onclick="exibir()">Exibir</button>
<script src="script.js"></script>
</body>
</html>
View my Javascript
//Função para pegar o dado
function Value1()
{
alert(localStorage.setItem('arquivo1', document.body.tarefa.value));
}
//Função para exibir o que foi pego
function exibir(){
alert("O valor guardado e: " + localStorage.getItem("arquivo1"))
}
//Função para apagar o que esta definido no localStorage
function limpar(){
localStorage.removeItem("arquivo1");
But when I click to display Alert, which is giving indefinite, I could not find a solution to this problem.
My new code is: Even so, it keeps appearing with null value.
//Função para pegar o dado
function Value1()
{
var tarefa = document.querySelector("#tarefa").value;
localStorage.getItem("arquivo1", tarefa);
console.log(tarefa);
}
//Função para exibir o que foi pego
function exibir(){
alert("O valor guardado e: " + localStorage.getItem("arquivo1"))
}
//Função para apagar o que esta definido no localStorage
function limpar(){
localStorage.removeItem("arquivo1");
}
can share the entire html? where is the element
tarefa
? The program is trying to access the value of this element, but is not succeeding, because it is not defined in the html you shared.– Lucas
Okay, I added all the html code
– rodolfo.martins
Your problem is here
document.body.tarefa.value
, there is no such syntax in Javascript, you can take the value ofinput
by id, tag, class, querySelector, querySelectorAll.– LeAndrade
I commented on another code, yet it keeps appearing as null.
– rodolfo.martins
Yes it will continue as
nulo
pq is taking input value by id Document.querySelector("#task"). value only in hisHTML
have no input with id task!– LeAndrade