I wanna callback jQuery function
$(function upload() {
.....
});
$("#select").change(function() {
upload();
});
Console Uncaught ReferenceError: upload is not defined
4 Answers 4
You have actually provided a named callback to the shortcut DOM ready event handler. You need to declare it globally (or in the same scope).
This example has them in the same scope:
$(function(){ // DOM ready handler
function upload() {
};
$("#select").change(function() {
upload();
});
});
As the function has no parameters it abbreviates to:
$(function(){ // DOM ready handler
function upload() {
};
$("#select").change(upload);
});
The cleaner way (if the function is not reused) is just to place the code in the change callback:
$(function(){
$("#select").change(function() {
// Do the upload code here instead
});
});
Comments
That is because it is inside a closure and therefore not global. Remove that $ wrapping and everything should be fine.
function upload() {
// (...) code
}
$("#select").change(function() {
upload();
})
Or put the <select> code inside the DOM ready wrapper. It should work too. Or declare it like
window.upload = function(){
// this is global
}
Comments
You have to put ("#select").change inside Ready function and upload function outside ready function.
function upload() {
// any thing you want to do after select
};
$( document ).ready(function() {
$("#select").change(function() {
upload();
})
});
Comments
Try this : define a function upload inside $(function(){ }); and call it inside change event handler of select box.
$(function(){
//define function
upload = function(){
....
}
$("#select").change(function() {
//call function
upload();
});
});
2 Comments
Note is incorrect. So long as it is defined in the same scope it will exist. Also it only needs to be defined before it is actually called. Javascript only checks at call-time :)note. Thanks :)