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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:08:58+00:00 2026-06-01T22:08:58+00:00

Here’s an example of my problem: HTML <p><a class=clickme href=#>I would like to say:</a></p>

  • 0

Here’s an example of my problem:

HTML

<p><a class="clickme" href="#">I would like to say:</a></p>

jQuery

$("a.clickme").click(function() {
    $('p').append('<div><a href="#" id="say-hello">Hello</a></div>');
});

$("#say-hello").click(function(){
    alert('test hello');
});

Here’s a fiddle with the example code:
http://jsfiddle.net/johnmorris/2UVYd/2/

The first click function fires just fine. But, the alert (or any other function) will not fire on the newly appended elements. I basically understand that the appended element didn’t exist on page load, so jQuery doesn’t “see” it… thus, the second click function not firing.

I’m just wondering if there’s a way around that. And, if so, what is it. I can’t seem to get this figured out.

  • 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-01T22:08:59+00:00Added an answer on June 1, 2026 at 10:08 pm

    You need to wait to assign the event until the element exists:

    $("a.clickme").click(function() {
        $('p').append('<div><a href="#" id="say-hello">Hello</a></div>');
        $("#say-hello").click(function(){
            alert('test hello');
        });
    });
    

    Notice that the click assignment for the newly created anchor is INSIDE the other click callback.

    @Vega is using another technique called “delegation” where you assign the click for “say-hello” to the p instead of the anchor itself. When the anchor is clicked the event “bubbles” up through the DOM and is gets caught by the event listener on the p tag. This works correctly, but this may not be the right time for delegation. Delegation is usually used when you have a collection of items inside of a common container (e.g. LIs in a UL) that all have to handle the same event. If you just have one “say-hello” then I would recommend not using delegation.
    ​

    If you need to be able to click the button multiple times and have it inserted multiple times then delegation might be the right option (but you’ll need to stop using a hard-coded ID). Or… you could do it this way:

    $("a.clickme").click(function() {
        $('<div><a href="#" class="say-hello">Hello</a></div>').find('.say-hello')
                .click(function(){
                    alert('test hello');
                })
            .end()
            .appendTo('p');
    });
    

    This code actually digs into the newly created elements and attaches the event before appending to the DOM. (Oh, and notice that I’m using a className rather than an id for “say-hello”.)

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

Sidebar

Related Questions

Here's my code in the <head></head> : <link rel=stylesheet href=http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.css /> <script type=text/javascript src=http://code.jquery.com/jquery-1.7.1.min.js></script>
Here is the code in a function I'm trying to revise. This example works
Here is an example: I write html code inside of textarea, then I swap
Here an example of my checkbox list http://jsfiddle.net/YnM2f/ Let's say I check on G
Here's a coding problem for those that like this kind of thing. Let's see
Here's an example list: <ul> <li><a href=#>Link 1</a></li> <li><a href=#>Link 2</a></li> <li><a href=#>Link 3</a></li>
Here's the view: @if (stream.StreamSourceId == 1) { <img class=source src=@Url.Content(~/Public/assets/images/own3dlogo.png) alt= /> }
Here is the Javascript I currently have <script type=text/javascript> $(function() { $('.slideshow').hover( function() {
Here is my problem : I have a post controller with the action create.
Here is an example. foreach (var doc in documents) { var processor = this.factory.Create();

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.