Simon
Simon

Reputation: 8345

Javascript - Uncaught SyntaxError: Unexpected token ) when calling a function

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?

Upvotes: 0

Views: 99

Answers (3)

Arun P Johny
Arun P Johny

Reputation: 388446

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
    });
}

Upvotes: 0

Vishnu Prasad
Vishnu Prasad

Reputation: 727

The ( will come after } brackets.

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

Upvotes: 0

Anik Islam Abhi
Anik Islam Abhi

Reputation: 25352

Try like this

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

Upvotes: 3

Related Questions