-1

I wanna callback jQuery function

$(function upload() {
.....
});
$("#select").change(function() {
 upload();
});

Console Uncaught ReferenceError: upload is not defined

dumbass
27.2k4 gold badges43 silver badges77 bronze badges
asked Oct 7, 2014 at 9:45

4 Answers 4

1

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
 });
});
answered Oct 7, 2014 at 9:47
Sign up to request clarification or add additional context in comments.

Comments

0

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
}
answered Oct 7, 2014 at 9:46

Comments

0

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();
})
});
answered Oct 7, 2014 at 9:49

Comments

0

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();
 });
});
answered Oct 7, 2014 at 9:47

2 Comments

Actually your 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 :)
@TrueBlueAussie, yeah you are correct.. I removed my note. Thanks :)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.