0

I am wanting to add a callback to some code.

Here is the code before hand:

var htmlParent2 = $('#testnohtml');
setHtml("/objects/data.html", htmlParent2);
$('#text', htmlParent2).html("test text");  

function setHtml(url, parent)
{
    $.get( url, function( data ) {
      parent.html( data );
    });
}    

Here is what I have written:

var htmlParent2 = $('#testnohtml');
setHtml("/objects/data.html", htmlParent2, function(result))
{
  $('#text', htmlParent2).html("test text");    
}

function setHtml(url, parent, callback)
{
    $.get( url, function( data ) {
      parent.html( data );
    });
}

I am getting the following error:

Uncaught SyntaxError: Unexpected token )

At this line of code:

setHtml("/objects/data.html", htmlParent2, function(result))

Can someone please help me with the correct syntax?

2
  • Yeah, function(result) is missing {} Commented Jun 24, 2015 at 5:15
  • Don't forget to call callback() Commented Jun 24, 2015 at 5:16

3 Answers 3

3

Try like this

setHtml("/objects/data.html", htmlParent2, function(result)
{
  $('#text', htmlParent2).html("test text");    
});
Sign up to request clarification or add additional context in comments.

Comments

0

Apart from the syntax error, just passing a callback is not enough, you also need to invoke it

var htmlParent2 = $('#testnohtml');
setHtml("/objects/data.html", htmlParent2, function (result) { //fix the syntax issue here
    $('#text', htmlParent2).html("test text");
})

function setHtml(url, parent, callback) {
    $.get(url, function (data) {
        parent.html(data);
        callback(); //you need to call the callback here
    });
}

Comments

0

The ( will come after } brackets.

setHtml("/objects/data.html", htmlParent2, function(result)
{
  $('#text', htmlParent2).html("test text");    
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.