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

  • Home
  • SEARCH
  • 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 8973699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:30:28+00:00 2026-06-15T18:30:28+00:00

Here is a jsFiddle to explain my problem. If you try to click on

  • 0

Here is a jsFiddle to explain my problem. If you try to click on the button “Add an ingredient”, the button click event is called. My problem is that when I click on the “Create a subtitle”, it dynamically created “Add an ingredient” button for each sub-categories. All button have the same id beginning with “create-ingredient” concatenated with a unique ID so it creates a unique ID for the button.
I then use a “start with” selector in jQuery to select all button that start with “create-ingredient”. It just doesn’t select the ones that are dynamically created, it just work with the one that was originally in the html page.

Here is the click event not firing up on the dynamic ones:

$("[name^='create-ingredient']").click(function() {
    alert('ingredient clicked');
    return false;
});​

What do I need to change so the dynamically created button event gets called?

Thanks!

  • 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-15T18:30:30+00:00Added an answer on June 15, 2026 at 6:30 pm

    click wires up the event statically. You want on, which triggers the event even on added elements.

    Note that the syntax for on is slightly different than click. You typically use:

    $("container").on("click", "selector", handler);
    

    where “container” selects a static container element (can just be “document”), and “selector” is the element you want to target.

    Forked Fiddle.


    Edit

    Clarifying the above per David’s comment: the use of on actually attaches an event handler to “container”, and delegates the callback to any elements inside the container that match “selector”. This is a case where it’s well worth reading the JQuery docs:

    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

Before I explain, here's the example HTML code : [ Demo on jsFiddle here.
To explain my question I have created this http://jsfiddle.net/Jams/XNVB2/ here I want when div1
My script is here : jsfiddle i have two attributes in my form that
my JSfiddle is here http://jsfiddle.net/hhimanshu/VAAKf/3/ When i click on search, it doesn't do anything,
I'd try to explain what I mean, but there is an easier way: click
OK this is fairly complicated to explain so I've put it online here: http://jsfiddle.net/zSgPr/10/
Fiddle here: http://jsfiddle.net/cgweb87/r3NEX/ When you click on the h2, it slides the div up
A bit hard to explain, so I've set up a jsFiddle here . Basically,
I'm having a problem with percentage based layout. Here is my code http://jsfiddle.net/uHkXH/ If
What solution can I find to prevent unnecessarily clicks activate function mouseup. here jsfiddle

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.