AnchovyLegend
AnchovyLegend

Reputation: 12538

Click trigger on anchor that is loaded by ajax

I am trying to use the ajax trigger on an anchor that is dynamically generated by ajax.

This is what I tried with no success and no errors:

        $('.excelDL').live('click', function(e){
            $.ajax({
                url: 'exceldl.php',
                data: $('#myForm').serialize(), 
                type: 'POST', 
                success: function(data){
                    $('.xldl').html(data);
                    attach = $('.xldl a').attr("href");

                    $('.xldl a').trigger('click');
                }
            });
        });

I believe it is because I am inserting new elements into the DOM using AJAX that trigger is not working, this is why I tried using live. I am not really sure how to proceed and solve this problem.

I appreciate any suggestions.

Many thanks in advance!

Upvotes: 0

Views: 435

Answers (1)

insomiac
insomiac

Reputation: 5664

Use "on" instead of "live"

    $(document).on('click','.excelDL',function(e){
        $.ajax({
            url: 'exceldl.php',
            data: $('#myForm').serialize(), 
            type: 'POST', 
            success: function(data){
                $('.xldl').html(data);
                attach = $('.xldl a').attr("href");

                $('.xldl a').trigger('click');
            }
        });
    });

jquery on documentation : http://api.jquery.com/on/

jquery live is deprecated after 1.7

Upvotes: 1

Related Questions