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 7006517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:29:17+00:00 2026-05-27T21:29:17+00:00

I am trying to use the JQuery on() method. Basically I am trying to

  • 0

I am trying to use the JQuery on() method. Basically I am trying to write an alert box similar to the one on here. If you’ve ever tried to vote on your own comment or thread, you’ll know what I’m talking about.

I have a JS function like below which simply returns my box with the message.

function alert_box(msg){
    return "<div class='alert-box'><p>" + msg + "<br /><i>( Click to Close )</i></p></div>";
}

I then call the alert-box like so

$(that).append( alert_box(data.err) );

The above works as expected but the problem is this function not triggering. The .alert-box gets added with the above code but doesn’t get removed with the below.

$('.alert-box').on({
    click: function() {
        $('.alert-box').remove();
    }
});

I assume its something to do with the .alert-box class being added after the DOM. Anyone got any pointers on this?

  • 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-27T21:29:18+00:00Added an answer on May 27, 2026 at 9:29 pm

    The form of .on() you were using does not handle dynamically added content. If you want to change it to handle dynamic content, then you can use this where you pass it a common parent object and then an argument for the selector to filter by:

    The prototype for that form of .on() is this: .on( events-map [, selector] [, data] ). And your code would work like this:

    $(document.body).on({
        click: function() {
            $('.alert-box').remove();
        }
    }, '.alert-box');
    

    In this new form, you’re attaching an event handler to a parent object and then looking for clicks in any children that match the ‘.alert-box’ selector. It’s best to pick a parent object that is as close to where your clicks will be, but one that doesn’t change. I used document.body here just because I know that exists and you haven’t shown your other HTML for me to know what else could be used.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use jquery.Ajax to post data to an ASP.NET MVC2 action method
I'm trying to use jQuery.scrollTo plugin with accordion (where one block expands after clicking
I'm trying to use jquery.qtip to an anchor element in jquery.datatables. Basically how it
Im ready to start pulling my hair out here. Trying to use jquery ajax
I'm currently trying to use the jquery find method on an object twice but
I'm trying to use JQuery to have a method fire when the following code
Trying to figure out how to use the Jquery .on() method with a specific
I am trying to use the jQuery setTimeout in order to call a method
I'm trying to use jQuery and Ajax and I use this method. but I
I'm trying to use JQuery to pass a data structure that looks similar to

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.