Reputation: 1489
I am trying to call a function inside of a button click event.
$('#btn_save').button().click(function(){
executeStatement();
});
$.fn.executeStatement = function(){
alert("Here!!!");
$.ajax({
url: 'api.php',
data: sqlStatement,
dataType: 'json',
success: function(data)
{
alert("Success!!!");
}
});
}
The problem is that it never gets called because it is undefined according to the debugger: Uncaught ReferenceError: executeStatement is not defined
Can someone give me a hint what is wrong? Thanks in advance
Upvotes: 0
Views: 75
Reputation: 616
Curt's answer is completely correct. But I thought I would add to it.
function executeStatement(){}
this is called a function declaration and will work in this case.
var executeStatement = function(){};
this is called a function expression and will also work in this scenario.
Upvotes: 3
Reputation: 126
Just to elaborate on the previous answer, $.fn is kind of like jquery's namespace. Anything that starts with $. is part of jquery. You can actually add functionality to jquery by adding functions to $.fn, but it's probably not what you want.
I found this really confusing at first too, so I thought I'd try to clarity.
Upvotes: 3
Reputation: 103428
Unless you are trying to make a jQuery plugin (which I don't think you are?), change your function declaration to:
function executeStatement(){
//code here
}
Upvotes: 5