Viktor
Viktor

Reputation: 722

Getting error when truing to save value from input to localStorage

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

Answers (3)

Z.Z.
Z.Z.

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

Thomas Orlita
Thomas Orlita

Reputation: 1627

You wanted to use innerHTML instead of html(). That's a jQuery function.

res.innerHTML = localStorage.getItem('value');

Upvotes: 1

NiallMitch14
NiallMitch14

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

Related Questions