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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:36:08+00:00 2026-06-02T20:36:08+00:00

How would I use .on() if the HTML is not generated yet? The jQuery

  • 0

How would I use .on() if the HTML is not generated yet? The jQuery page says

If new HTML is being injected into the page, select the elements and attach event handlers after the new HTML is placed into the page.

But I’m not really sure how to do that. Is there a way to “reload” the event handler?

So if I had

$(document).ready(function(){
    $('.test').on('click', function(){
        var id = $(this).attr('id');
        console.log("clicked" + id);
    });
generatePage();
});

where generatePage() creates a bunch of divs with .test, how would I rebind .on()?

I’m aware similar questions have been asked, but I didn’t find what I was looking for after a quick search.

  • 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-06-02T20:36:09+00:00Added an answer on June 2, 2026 at 8:36 pm

    Use .on like in the example below. One can presume that the body-tag is always available so it’s safe to attach the event handler to body and delegate the events to the selector, in this case .test.

    $(document).ready(function(){
        $('body').on('click', '.test', function(){ // Make your changes here
            var id = $(this).attr('id');
            console.log("clicked" + id);
        });
    
        generatePage();
    });
    

    Or if generatePage() is also generating the html, head and body tags use document as your selector.

    $(document).ready(function(){
        $(document).on('click', '.test', function(){ // Make your changes here
            var id = $(this).attr('id');
            console.log("clicked" + id);
        });
    
        generatePage();
    });
    

    According to the jquery documentation .on accepts the following parameters:

    .on( events [, selector] [, data], handler(eventObject) )
    

    Including selector is described as follows:

    When a selector is provided, the event handler is referred to as
    delegated. The handler is not called when the event occurs directly on
    the bound element, but only for descendants (inner elements) that
    match the selector. jQuery bubbles the event from the event target up
    to the element where the handler is attached (i.e., innermost to
    outermost element) and runs the handler for any elements along that
    path matching the selector.

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

Sidebar

Related Questions

What are the main reasons someone would use HTML's tbody in a table? Is
I would like to use HTML 4.01 Strict, and used a DOCTYPE of it
my question is how would I use an HTML::Template tag inside a value of
I would like to use a textarea in html form to get the delimited
I would use Jmagick into an Android Project. Is it possible? I have to
I would like to be able to create a page using HTML and scripts
I would like to use an xml file to create a html email. I
I have some static html content (included on a dynamically generated page) that I
I have a dynamically generated html page which is designed to be printed. I
I am aware that regex is not ideal for use with HTML strings and

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.