neiza
neiza

Reputation: 265

Save content of a div in localstorage after content loads

I load a page in a div called records via ajax and i want to save the content of the records div into the localstorage after its loaded into the div. With my currently script what it does is it already saves the empty content of the div before the loaded content comes up.

$(document).ready(function() {
    $('#records').load('records.php');
    localStorage.setItem('records', $records.html());   
});

Upvotes: 1

Views: 824

Answers (1)

bhspencer
bhspencer

Reputation: 13560

jQuery.load takes a callback function as its second param. From the docs:

If a "complete" callback is provided, it is executed after post-processing and HTML insertion has been performed.

That function is called when the load is finished. You just need to put your storage code in that callback.

$(document).ready(function() {
    $('#records').load('records.php', function() {
        localStorage.setItem('records', $records.html());
    });
});

Upvotes: 1

Related Questions