Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery events and .load()

Never ran into this problem with jQuery before. I have the following:

$(document).ready(function() {
    $('#browser_cat a').click( function() {
        $('#browser_cat').hide();
        $('#browser_cat').load('/api/browser/catchildren/'+$(this).attr('id'));
        $('#browser_cat').fadeIn();
        return(false);
    })
});

Pretty simple stuff. And it works, however the data gathered by .load() doesn't retrigger the event. It is a drill-down category type thing ... so level-1 does the load but then leve-2 bails to the href rather than triggering the click() event again.

The loaded HTML is a #browser_cat with links, and the generated DOM is OK.

I appriciate this is elementary. Thanks in advance.


Answer modified code:

$(document).ready(function() {
    $('#browser_cat a').live("click", function() {
        $('#browser_cat').hide();
        $('#browser_cat').load('/api/browser/catchildren/'+$(this).attr('id'));
        $('#browser_cat').fadeIn();
        return(false);
    })
});
like image 548
Aiden Bell Avatar asked Jan 30 '26 17:01

Aiden Bell


1 Answers

Use live event

Attach a handler to the event for all elements which match the current selector, now or in the future.

Replace

$('#browser_cat a').click( function() {

with

$('#browser_cat a').live("click", function() {
like image 72
rahul Avatar answered Feb 01 '26 06:02

rahul



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!