BlackFire27
BlackFire27

Reputation: 1550

jquery binding events to link fails

I have got this jquery code:

$(document).ready(function(){
$('a').bind('click', false);
var url;
$('a').on('click',function(e){
    url=$(this).attr('href'); //;
    $('#AjaxPage').load(url+" .AjaxContent");
});

});

The problem is the click event works the first time .. but on the content loaded from the loaded function , the links there dont have the click event i binded .. why is that? I thought you use 'on' as you would use 'live'

Update:

Now my load function doesnt get executed:

   $(document).ready(function(){


$('a').bind('click', false);
    var url;


$(document).on('click', 'a',function(){
        url=$(this).attr('href'); //;
        $('#AjaxPage').load(url+" .AjaxContent");
    });
});

Upvotes: 1

Views: 79

Answers (1)

Denys Séguret
Denys Séguret

Reputation: 382160

Replace

$('a').on('click', function(e){

with

$(document).on('click', 'a', function(e){ // replace document with a more precise selector if available

Contrary to live, on will handle events in a jQuery collection and delegates the event to the target verifying the supplied selector. The jQuery collection must be non empty. It can be more precise than document, of course, but must be a parent of the element you're adding and on which you wish to intercept the event.

Upvotes: 3

Related Questions