Susheel Singh
Susheel Singh

Reputation: 3854

unable to run script on dynamically json parsed content

I would like to give a high level explanation of what's my issue because I can't put up complete code which is too complex.

I have a button. when I click on the button, it pulls data from fb parses it and adds it to the page. Now after it is loaded to then page.. I want to run a script on the loaded content. I am running the script after the data is parsed but somehow it shows that element is not loaded by the time script started running. can some one throw some light.

$("somebutton").on("click",function(){
    LoadAndParseFbData();
});

function LoadAndParseFbData(){
    //loaded the json, parsed it and added to the page.
     anotherScript();
}

function anotherScript(){
    // this has some script related to data which is loaded dynamically by parsing json.
}

This is what i am trying on high level. please help thanks :)

Upvotes: 1

Views: 84

Answers (2)

Susheel Singh
Susheel Singh

Reputation: 3854

$("somebutton").on("click",function()
    LoadAndParseFbData();
});

function LoadAndParseFbData(){  
    FB.api(path, method, params, function(){
        //loaded the json, parsed it and added to the page.
        anotherScript();
     });         
}

function anotherScript(){
    // this has some script related to data which is loaded dynamically by parsing json.
}

Upvotes: 1

RozzA
RozzA

Reputation: 609

Run anotherScript() from within the ajax success callback:

$J.ajax({
    url: url2, 
    type: "post",
    data: { data: "yourdata",  },
    success: function (data, textStatus, jqXHR) {
        anotherScript();
    }, 
    error: function(jqXHR, textStatus, errorThrown) {
        /*error handling code here*/
    }
});

Upvotes: 0

Related Questions