Jquery dynamic button : how to bind to exisitng click event by class
<p>I have a click event triggered by the class selector inside the jquery ready scope:</p>

<pre><code>    $(".testBtn").click(function()
    {
        alert("This Works");      
    });
</code></pre>

<p>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?</p>

<p>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.</p>

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

    $(".testBtn").click(function()
    {
        alert("This Works");      
    });
});
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;table id="mainTable"&gt;
&lt;button class="addRowBtn"&gt;Add button&lt;/button&gt;
&lt;tr&gt;&lt;th&gt;Testing&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;button class='testBtn'&gt;Working&lt;/button&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;
&lt;/body&gt;

&lt;/html&gt;
</code></pre>
