I has something like this:
<div id="content"></div>
and JS:
function getData(id) {
$.getJSON('url/to/jsondata',{id: id}, function(data) {
/** append some data to div#content **/
$('#content').data('key', 'value');
});
then I want to get data of #content in script:
$(document).ready(function() {
getData(1); // run getData with some id
console.log($('#content').data('key'));
});
Problem is console.log not run after callback of getJSON finish, so i get a undefined value in log.
Which is right way to make it work?
Thank!
Return the deffered object from the ajax function and use the always, done or fail methods on it: