jquery - event.preventDefault() within inner function
<p>It seems that I miss out some basic concept of jquery, since this script 
won't do what I want it to do: </p>

<pre><code>    $(document).ready(function(){

    $('#form').submit(function(event) {
    console.log( "submitted form, checking #domain_url_input ");
    // event.preventDefault(); 
    var submit_error=false; 
    var is_primary_url = false;
    var url = $('#domain_url_input').val();

    var get_url = '/i.php?a=check&amp;url2check=' + escape(url);
    var valid_data = $.ajax({
        event: event,
        url: get_url,
        success: function(response) {
            // alert('ajax successful'); 
            is_primary_url = $(response).find('#check_div').text(); 
            // alert(is_primary_url);

            var dns_zone    = $('#dns_zone_sel').val(); 

            if (is_primary_url == 'YES' &amp;&amp; dns_zone=='') 
                { 
                event.preventDefault();
                alert('Choosing a DNS ZONE is required!');
                $('#dns_zone_sel').focus(); 
                this.submit_error=true; 
                // if (submit_error==true) event.preventDefault(); 
                // return 'error'; 
                } // 

            } // end func success
        }); // ajax  

    }); // submit

    }); // rdy
</code></pre>

<p>(I reduced the code a bit of course.)</p>

<p>The code works perfectly, except for the:</p>

<p>event.preventDefault();</p>

<p>won't take effect.</p>

<p>IF i put it into the second line right after </p>

<p>$('#form').submit(function(event) {... </p>

<p>it will work though.</p>

<p>The ajax-call works, I get a positive result.
It is as if it would not be possible to PREVENT the SUBMIT any more? </p>

<p>The Alert after:</p>

<p>event.preventDefault();
alert('Choosing a DNS ZONE is required!');</p>

<p>works perfectly, meaning, the script works, except for that the preventDefault()
does not have an effect.
That may be due to the event "event" to which I don't have proper access? </p>

<p>The intent is: </p>

<p>I want to stop the submit event.
I older js scripts I did that via "return false;"
but that won't work either.
Btw: I know the difference between preventDefault() and "return false",... I am just adding the information that I tried. In this case the "return false" would return from the $(...).ajax() to the $(...).submit() event.</p>

<p>Point is: the form submit() is already in progress when the $().ajax part is still doing the request. So somehow I need to make the submit() wait until the ajax is finished?</p>

<p>Thanx for any ideas!</p>

<p>AFTER ALL THIS following <strong>SOLUTION</strong> WORKS excactly how I wished - Thanks to my friend Andreas, @Cory and @Michael:</p>

<pre><code>`&lt;script type="text/javascript"&gt;
$(document).ready(function(){
var disableSubmit   = false;
var transmitNow     = false;
$('#form').submit(function(event) {
    console.log( "submitted form, checking #domain_url_input ");
    console.log( "disableSubmit=" + disableSubmit); 
    if( transmitNow === true ) return;  
    event.preventDefault(); 
    if (disableSubmit ===true) return; 
    $('#form').prop( 'disabled', true );
    var submit_error=false; 
    var is_primary_url = false;
    var url = $('#domain_url_input').val();
    if (url=='') { alert('Error! A domain/url must be provided!'); return false; } 

    var get_url = '/i.php?a=check&amp;ajax=1&amp;url2check=' 
                        + escape(url) + '&amp;t='+ Date.now();

    disableSubmit   = true; // Formular ausgrauen .... als todo 
    var valid_data = $.ajax({
        url: get_url,
        error: function(){ disableSubmit = false; $('#form').prop( 'disabled', false ); },
        complete: function(){ disableSubmit = false; $('#form').prop( 'disabled', false ); },
        success: function(response) {
            disableSubmit = false; 
            $('#form').prop( 'disabled', false );

            is_primary_url = $(response).find('#is_url_primary_check_div').text(); 

            var registrar   = $('hd[registrar]').val(); 
            var webserver   = $('hd[webserver]').val(); 
            var dns_zone    = $('#dns_zone_sel').val(); 

            if (is_primary_url == 'YES' &amp;&amp; dns_zone=='') 
                { 
                // event.preventDefault();
                alert('You entered a primary domain. Choosing a DNS ZONE is required!');
                $('#dns_zone_sel').focus(); 
                submit_error=true; 
                }   
            if (submit_error != true ) 
                { 
                transmitNow = true; 
                $('#form').submit();
                }  
            } // end event func success
        }); // ajax  

    }); // submit

}); // rdy
    &lt;/script&gt;`
</code></pre>

<p>THANX TO ALL!! :) <em>happy</em>!</p>