[Solved] How do make jquery code executed within order


JQuery will execute the event handlers in the order they are bound, but as @KevinB has pointed out in a comment, the one in the <body> is bound first because that code is executed as it is encountered, while the one in the <head> is bound second because it is executed during the page-ready event.

You could rearrange the code so the two handlers are bound in the correct order and then use e.stopImmediatePropagation() in the first handler when you want to prevent the second from executing, but it would be better to bind just a single handler.

Just place one <script> block in the <head > and bind one submit handler.

<script type="text/javascript">
    $(document).ready(function() {
        $('form').submit(function(e) {
            if (!$('.text').val()) {
                e.preventDefault();
                console.log('error');
                $.ajax({
                    url: 'send.php',
                    type: 'post',
                    success: function() {
                        console.log('done');
                    }
                });
            }
        });
    });
</script>

1

solved How do make jquery code executed within order