Search code examples
jquerykeyboard-eventspreventdefaultpjax

jquery PJAX/AJAX preventDefault() not blocking link from being clicked


I'd like to prevent PJAX clicking of a link unless an input box is keyed up.

Here's the code:

$('a.pjax').pjax({container: '#main_content'}).live('click', function(event){ 
   if(keyed){ 
        console.log('yes, you typed');
   }
   else if(keyed==false){
        console.log('no, please type something');
        event.preventDefault();                                                 
   }
}); 

My problem is that despite the condition being determined correctly, PJAX still loads the page regardless of the preventDefault().

Any thoughts on why this is not working?


Solution

  • It seems the pjax function will always fire with the set-up you have in place.

    However, the following should work:

    $(document).on('click', 'a.pjax', function (event) {
        if (keyed) {
            console.log('yes, you typed');
            return $.pjax.click(event, '#main_content');
        }
        else {
            console.log('no, please type something');
            return false;
        }
    });