Ankur
Ankur

Reputation: 51100

jQuery, how to call a function while passing a variable

I want to use jQuery's .get method to send an ajax call to the server.

I am using this line:

$.get("InfoRetrieve", { },addContent(data));

As you can see I want to call a function call addContent and pass it the data that is retrieved from the server.

The function addContent is below:

function addContent(data){
    $("#0001").append(data);
}

It doesn't seem to work, can you see why.

Upvotes: 0

Views: 238

Answers (2)

Roy Tang
Roy Tang

Reputation: 5761

Try wrapping it in a new function object:

$.get("InfoRetrieve", { },function() { addContent(data) });

Upvotes: 0

Doug Neiner
Doug Neiner

Reputation: 66191

Just change it to:

$.get("InfoRetrieve", { },addContent);

It will take care of passing data when it calls the function.

Upvotes: 6

Related Questions