0

I was working on a Login Form ( AJAX ) enabled. When a use clicks on link, that Login box, as a floating modal appears. The Content of that Login box is loaded from another page using jQuery's $().load function. On click of the link the Login Box loads. But, when I implemented AJAX submitting on that form:

$(document).ready(function(){
    $("form#loginbox").submit(function(e){ e.preventDefault(); /*AJAX function here*/ });
});

But the form is submitted normally, without following what is given in the function, that is ( Prevent the Default - Submitting and use AJAX ). so does that mean the Loaded content is not considered inside DOM or the script cannot read it? I load the content only once:

$(document).ready(function(){
    if($("#loadedform").html() == "")
    { $(this).load('/load/login.html'); }
    $(".login-modal').slideDown('slow');
});

The structure:

<div class="login-modal"><div id="loadedform"></div></div>

the content to be loaded:

<form id="loginbox" method="post" action="xyz.html">
<!-- INPUT Boxes and Submit Button -->
</form>

4 Answers 4

2

Change

$("form#loginbox").submit(function(e){ 
    e.preventDefault(); 
    /*AJAX function here*/ 
});

to

$('#loadedform').on("submit", "form#loginbox", function(e){ 
    e.preventDefault(); 
    /*AJAX function here*/ 
});

or if you're using a version before 1.7:

$('#loadedform').delegate("#loginbox", "submit", function(e){ 
    e.preventDefault(); 
    /*AJAX function here*/ 
});

This is because the event is registered when the page is loaded, so when your form is loaded it does not have the event attached. This change will register the event at a higher level in the dom so any new content will also work.

It also looks like you should change the code that loads the form to:

if($("#loadedform").html() == "") { 
    $("#loadedform").load('/load/login.html'); 
}
$(".login-modal').slideDown('slow');

Otherwise you won't be loading it into the right place.

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

Comments

1

Scripts on a page retrieved using .load() are not run, so your first script that tries to make the form submit via AJAX won't be run.

Instead, move that first script so it runs when .load() completes, using a callback:

$(document).ready(function(){
    if($("#loadedform").html() == "") { 
         $(this).load('/load/login.html', function() {
             $("form#loginbox").submit(function(e){ e.preventDefault(); /*AJAX function here*/ });
         }); 
    }
    $(".login-modal').slideDown('slow');
});

Also, try not to use a bracket style like you have in Javascript:

// some statement that begins a block here
{ doStuff(); }

This is because the Javascript interpreter can insert semi-colons after that first line sometimes, which can break your code.

See the section on Automatic Semicolon Insertion in the Javascript Garden page for more details.

2 Comments

Thanks. About your advice ( the block thing ) -> So, what happens when we compress the JavaScript files using a compressor like JSMin?
@AbhishekBiswal I believe, though I have not confirmed this, that those compressors automatically insert explicit braces ({, }) in your code. Which, it occurs to me, could again cause issues if it incorrectly guesses where they should go.
0

You have to delegate over the dynamic elements using .on() method

$(document).on("submit","#loginbox", function() {
 e.preventDefault(); 
 /*AJAX function here*/
});

1 Comment

It's worth pointing out that using document as the static element you are binding event handlers to is not recommended, as the performance will suffer if you have too many such handlers. This is the main reason why jQuery moved away from .live(), which used exactly that technique. Instead, they recommend binding to a static element much closer to the dynamically added content, to save the event from having to bubble up quite so far. See the Event Performance section on the documentation for .on() for more details.
0

e.preventDefault stops or cancels the current event if it can be cancel, however the submit event will continue to propagate up the DOM and can cause the submit to fire anyway. If you are really replacing the default functionality of the button you can either do both e.preventDefault and e.stopPropagation or you can just return false.

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.