1

I'm using jQuery's load() function to change content on a page dynamically. It's a application with different 'levels' but the URL never changes.

It's working fine except for the final screen I'm using. I've got a form with text inputs etc that I need to create some form validations for but I can't get on('submit') to work after the page load. I am using a callback function on the load function and I do run some simple jQuery CSS, etc. to content that is on the final_page.php so I know that it's successfully running the function AFTER the content is loaded.

I'm trying to simply just apply a preventDefault() to the form for now to confirm that it's working correctly but the form submits every time I get to that final page so it looks like the on() function doesn't get bound to the form selector.

Is there some sort of conflicting with submit and load?

$('#main_content').load('final_page.php', function(){
    $('#main_content').css({'height':'auto','padding-bottom':'25px','background':'#086c8c'});
    $('#hidden_input').attr('value', 'complete');

    //GOOD UP TO THIS POINT

    $('#final_form').on('submit',function(e){               
        e.preventDefault();
    });
});
9
  • That code looks fine, which tells us that either your form doesn't have id="final_form" or you have more than one element with that id (which is invalid). Commented Apr 2, 2014 at 6:13
  • I guess this post has your answer stackoverflow.com/questions/18545941/… Commented Apr 2, 2014 at 6:14
  • @theinsaneone: Delegation is a useful approach, but the OP code would work if the form had that id (and the id is unique, as required). Delegation isn't required, and may or may not be useful depending on what the actual problem is. Commented Apr 2, 2014 at 6:15
  • can you log the value of $('#final_form').length to see whether the element is present using console.log('testing', $('#final_form').length, $('[id=final_form']).length) inside the load callback Commented Apr 2, 2014 at 6:17
  • Good call @ArunPJohny, I'll try that now. Commented Apr 2, 2014 at 6:38

0

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.