Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5989715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:08:28+00:00 2026-05-22T23:08:28+00:00

How can I get jQuery event listeners to listen to elements loaded through ajax

  • 0

How can I get jQuery event listeners to listen to elements loaded through ajax calls?

I load comments into my document via ajax. Each comment has an ajax delete button. For comments that were loaded originally with the document, my function works fine.

But any newly created comments, or comments retrieved through ajax, the event lister does not pick-up on them.

<a class="comment-delete" href="/comments/delete/124">delete</a>

$(".comment-delete").click(function(e) {
    e.preventDefault();

    var $link = $(this),
        url = $link.attr('href');

        $.ajax(url, {
            dataType: 'json',
            beforeSend: function() {
            },
            error: function() {
            },
            success: function(data) {
                if (data.success) {
                    $($link).parent().parent().hide();
                }
            }
        });
});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T23:08:29+00:00Added an answer on May 22, 2026 at 11:08 pm

    Updated Answer

    Today, the appropriate way to do this is to use the on method with an additional selector. For instance, if we wanted to listen for any click on images we could do this:

    $("img").on("click", function () {
        alert( "You clicked on " + this.src );
    });
    

    This will not work for any image elements added to the DOM after this code was ran. Instead, we need to use event delegation, meaning we handle the event further up the DOM in its bubbling phase:

    $(document).on("click", "img", function () {
        alert( "You clicked on " + this.src );
    });
    

    In this case, we handle the event at the document level, but only if the element it proceeded from matches our second "img" selector. So any image, whether it was on the page initially, or dynamically loaded in at a later time, will now be handled.

    Original Answer (Written in 2011)

    $.live() and $.delegate() are the two methods you should be looking to. The first will listen to the document for any new elements matching your selector, and the second will provide a more granular scope for where it ought to be listening.

    $(".comment-delete").live("click", function(){
      // handle delete logic
    });
    

    Or, the more specific:

    $(".comments").delegate(".comment-delete", "click", function(){
      // handle delete logic
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Not sure what I'm doing wrong but I can't get my JQuery AJAX call
Using jQuery I can easily get the number of DOM elements used by a
I'm trying to get my jQuery event callback to trigger correctly, but I can't
If i do jQuery(expr).change( function), then I can get an event function to fire
How can I get jQuery to only do an ajax query one time? Right
I can't find any solution to get this jquery events to work. $(window).one('click',function(){ console.log('hide
Is there any way I can get jQuery to perform a function when you
How can I get jQuery to return a html element exactly the way that
I can't get the jQuery datepicker to work. I have followed everything from the
Can't seem to get jQuery to change my css. In my css file this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.