I’m still learning JQuery (and as a result a little JavaScript) but I can’t seem to find out how to use a previously defined function in a callback.
Say I have:
<script>
$(document).ready(function() {
function ajax_start() {
alert("starting...");
}
});
</script>
And I wish to use this in another function e.g:
<script>
$(document).ready(function() {
$.ajax({
beforeSend: ajax_start(),
url: "insert_part.php",
type:"POST",
data: "customer="+customer
});
});
</script>
Would this be correct? (I assume not as it doesn’t…) what is the proper way of doing a callback?
Close.
You need to do this because
ajax_start()evaluates to the value returned by executing the function namedajax_start, butajax_startevaluates to the function itself.Edit re: OP comment
There are a couple ways to do it. Combine them using an anonymous function:
Or just declare a named function that does what you want, and then use it: