Affan Ahmad
Affan Ahmad

Reputation: 451

How to save Editable div content in local storage.

I am try to make small application there i have some editable dives so now i need to make setup if user refresh page than editable dives content store in local storage via JavaScript and update local storage after every 20 sec.Is that possible.

For example.

HTML

<div contentEditable='true'; >Job Title</div>

<div contentEditable='true'; >Email/Other</div>

Upvotes: 0

Views: 3202

Answers (3)

Ali Sarfaraz
Ali Sarfaraz

Reputation: 80

    <script type="text/javascript">

    setInterval(
        function(){
                if(typeof(Storage)!=="undefined")
              {
                  localStorage.lastname=document.getElementById("result").value;
                  document.getElementById("result").innerHTML="Last name: " + localStorage.lastname;
                }
            else
            {
                alert("Sorry, web storage not supported");
             }
             },3000
         );

    </script>

try this code it works on every 3 seconds you can increase 3000 to 20000 for 20 seconds

Upvotes: 0

Paul Draper
Paul Draper

Reputation: 83205

HTML

<div id="job" contentEditable="true"></div>
<div id="email" contentEditable="true"></div>

Javascript

 document.getElementById('job').innerHTML = localStorage['job'] || 'Job Title';
 document.getElementById('email').innerHTML = localStorage['email'] || 'Email/Other';

 setInterval(function() {
      localStorage['job'] = document.getElementById('job').innerHTML;
      localStorage['email'] = document.getElementById('email').innerHTML;
 }, 20 * 1000);

Upvotes: 3

rabisg
rabisg

Reputation: 731

setInterval(function(){
  localStorage.jobTitle = document.getElementById('div-id').innerHTML;
  localStorage.email = document.getElementById('the-other-div-id').innerHTML;
}, 20000);

Upvotes: 0

Related Questions