How can I apply jQuery table stripes after dynamically adding new rows?
<p>I'm having trouble figuring out how to maintain my table stripes after adding new rows to a table. If I change the <code>.insertAfter()</code> selector to <code>#names tr:last</code> it works as I would expect, however using <code>tr:first</code> or <code>tr:nth-child(x)</code> results in the new row and all rows below it being striped. When looking at the updated DOM in Chrome's developer tools everything looks normal. I'm hoping someone can explain to me why this isn't working. I feel like I must be missing something fundamental. Here's the code:</p>

<pre><code>&lt;!DOCTYPE html&gt;
&lt;html&gt;
    &lt;head&gt;
        &lt;style type="text/css"&gt;
            .striped {
                background-color: #silver;
            }
        &lt;/style&gt;

        &lt;script type="text/javascript" src="jquery-1.4.3.js"&gt;&lt;/script&gt;
        &lt;script type="text/javascript"&gt;
            function stripes () {
                $("tr:even").each(function () {
                    $(this).addClass("striped");
                });
            }

            $(function () {
                stripes();
                $("#addRow").click(function () {
                    $("&lt;tr&gt;&lt;td&gt;Kenny&lt;/td&gt;&lt;td&gt;Bania&lt;/td&gt;&lt;/tr&gt;").insertAfter("#names tr:first");
                    stripes();
                });
            });
        &lt;/script&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;div&gt;
            &lt;a href="#" id="addRow"&gt;Add Row&lt;/a&gt;
            &lt;table&gt;
                &lt;thead&gt;
                    &lt;tr&gt;
                        &lt;th&gt;First Name&lt;/th&gt;
                        &lt;th&gt;Last Name&lt;/th&gt;
                    &lt;/tr&gt;
                &lt;/thead&gt;
                &lt;tbody id="names"&gt;
                    &lt;tr&gt;
                        &lt;td&gt;Jerry&lt;/td&gt;
                        &lt;td&gt;Seinfeld&lt;/td&gt;
                    &lt;/tr&gt;
                    &lt;tr&gt;
                        &lt;td&gt;Elaine&lt;/td&gt;
                        &lt;td&gt;Benes&lt;/td&gt;
                    &lt;/tr&gt;
                    &lt;tr&gt;
                        &lt;td&gt;George&lt;/td&gt;
                        &lt;td&gt;Costanza&lt;/td&gt;
                    &lt;/tr&gt;
                    &lt;tr&gt;
                        &lt;td&gt;Cosmo&lt;/td&gt;
                        &lt;td&gt;Kramer&lt;/td&gt;
                    &lt;/tr&gt;
                &lt;/tbody&gt;
            &lt;/table&gt;
        &lt;/div&gt;
    &lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>Thanks!</p>
