0

When I use this code it bind my $.post() to a form and prevent a page reload for submitting it:

$("#analyze_audio").submit(function (e) {
    debugger;
    e.preventDefault();
    var url = "/analyze_audio";
    var dataToSend = {"analyze_audio": "analyze_audio"};
    var callback = function (data) {
        debugger;
        document.getElementById("audioresponse").innerHTML = (data);
    };
    $.post(url, dataToSend, callback, 'html');
});

But it doesn't trigger the debuggers I used in it, so it doesn't bind the event to my function correctly, but when I use this code fragment, it works perfectly:

$(function() {
    $("#analyze_audio").submit(function (e) {
        debugger;
        e.preventDefault();
        var url = "/analyze_audio";
        var dataToSend = {"analyze_audio": "analyze_audio"};
        var callback = function (data) {
            debugger;
            document.getElementById("audioresponse").innerHTML = (data);
        };
        $.post(url, dataToSend, callback, 'html');
    });
});

I really don't understand why?

1
  • Thats because the first is the wrong way, and the second is the right way. Thats all there is to it! Commented Sep 7, 2015 at 14:08

3 Answers 3

6

When you're working with jQuery (which you clearly are), wrapping a function in $( ) makes it a function that's called by the library when the "DOM ready" event is received from the browser. It thereby defers execution of your code until the DOM is fully built. That makes your code work because it ensures that the element with id "analyze_audio" is present.

There's nothing syntactically special about $( ) — it's just a simple function call, to a function named $ (which is the main entry point to the jQuery library).

You may see code that does something similar:

$(document).ready(function() { ... });

That does precisely the same thing (and is also a jQuery idiom). There's no reason to use that form unless you enjoy typing in extra characters.

Sign up to request clarification or add additional context in comments.

Comments

3

$(function() {}); is just a shortcut for document.ready. It would work the same like this:

<script type="text/javascript">
        $(document).ready(function() {
            $("#analyze_audio").submit(function (e) {
                debugger;
                e.preventDefault();
                var url = "/analyze_audio";
                var dataToSend = {"analyze_audio": "analyze_audio"};
                var callback = function (data) {
                    debugger;
                    document.getElementById("audioresponse").innerHTML = (data);
                };
                $.post(url, dataToSend, callback, 'html');
            });
        });
    </script>

Comments

1

When you bind event to the form #analyze_audio it not present in DOM yet. If you put your script after the html with form, then it will work. Or you can use $(document).ready() to add your binding or just $(function(){}) both this functions will be executed when whole page will be loaded.

Comments

Your Answer

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.