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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:38:40+00:00 2026-06-15T16:38:40+00:00

I have a click event triggered by the class selector inside the jquery ready

  • 0

I have a click event triggered by the class selector inside the jquery ready scope:

    $(".testBtn").click(function()
    {
        alert("This Works");      
    });

This works fine for static buttons how ever this doesn’t work for dynamic buttons which are added upon clicking the button with “addRowBtn” id. I’m guessing that it has something to do with that the button is created after the event is registered but still the new button has the ‘testBtn’ class so it makes sense it should work.
Any idea what am i doing wrong and how to make the dynamic buttons registered to the class selector click event?

Here is the whole code, you can copy and paste into an html, click the ‘add button’ and try to click the added buttons. you’ll see nothing happens.

<html>
<head>
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js'></script>
<script src='http://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js'></script>
<script>
$(function()
{    
    $(".addRowBtn").click(function()
    {
        $("#mainTable tr:last").after("<tr><td><button class='testBtn'>NotWorking</button></td></tr>");           
    });

    $(".testBtn").click(function()
    {
        alert("This Works");      
    });
});
</script>
</head>
<body>

<table id="mainTable">
<button class="addRowBtn">Add button</button>
<tr><th>Testing</th></tr>
<tr><td><button class='testBtn'>Working</button></td></tr>
</table>
</body>

</html>
  • 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-15T16:38:41+00:00Added an answer on June 15, 2026 at 4:38 pm

    The standard answer here is event delegation. Hook click on the parent of all of these buttons, and ask jQuery to notify you only when the actual element clicked matches the selector, like this using delegate:

    $("#mainTable").delegate(".testBtn", "click", function(e) {
        // ...
    });
    

    …or like this in jQuery 1.7+ using the hyper-overloaded on:

    $("#mainTable").on("click", ".testBtn", function(e) {
        // ...
    });
    

    Note the difference in the order of arguments. I prefer delegate for the explicitness of it.

    Live Example | Source

    In either case, jQuery will hook click on #mainTable, but when the click occurs, only call your handler if an element matching .testBtn is in the lineage between the element that was clicked and the #mainTable. It will call the handler as though it had been hooked to the .testBtn element.

    Note that you can do the same for your .addRowBtn if you like.

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

Sidebar

Related Questions

I have the following click event $(.element).click(function() { var idNum = $(this).getID; $('#new-'+idNum).fadeIn(); });
If you bind a click event to a class: $(.testClass).click( function() { $( this
EDIT** I have this click event $('.next-question').click(function () { $('td').removeClass('highlight-problem'); var r = rndWord;
i have a click event for $('#blah div'). div has text inside of it
I have a menu click event that looks something like this.... Public Sub ToolbarManager_ToolClick(sender
If I have a jQuery .click() event firing for both body and a div,
I've got the following in my <head> section: <script type=text/javascript> $(function(){ $(h5).live(click, function(){ alert($(this).tmplItem());
I just ran across some jQuery that looks like this: $('.add-row').live('click.add', function() { //
I have a click event handler in class A with some logic. And now
I have found that dragend event in Google Map also trigger the click event

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.