Reputation: 722
Getting error when truing to save value from input to localStorage
res.html is not a function
Here is my code:
Enter Text: <input type="text" id="inp" onchange="myFunction()">
<div id="result"></div>
<script>
var inp = document.getElementById('inp');
var res = document.getElementById('result');
function myFunction() {
var str = res.innerHTML = inp.value;
localStorage.setItem('value', str);
if(localStorage.getItem('value')) {
res.html(localStorage.getItem('value'));
}
}
Upvotes: 0
Views: 58
Reputation: 704
It is not localStorage issue. html is not a defined function in DOM
You can do:
if(localStorage.getItem('value')) {
res.innerHTML = localStorage.getItem('value');
}
Upvotes: 1
Reputation: 1627
You wanted to use innerHTML
instead of html()
. That's a jQuery function.
res.innerHTML = localStorage.getItem('value');
Upvotes: 1
Reputation: 1229
.html()
is a JQuery function not a JavaScript one. To achieve what you're doing using JavaScript, you would use .innerHTML
. So for example:
res.innerHTML = localStorage.getItem('value');
Upvotes: 1