Javascript tr click event with newly created rows

Posted by yalechen on Stack Overflow See other posts from Stack Overflow or by yalechen
Published on 2012-06-28T21:14:22Z Indexed on 2012/06/28 21:15 UTC
Read the original article Hit count: 228

I am very new to web development. I am currently using tablesorter jquery plugin to create a dynamic table, where the user can add and delete rows. I am having trouble with changing the background color of newly created rows upon clicking. It works fine with rows that are hard coded in html. Here is the relevant code:

$(document).ready(
    function() {
        $('table.tablesorter td').click(
            function (event) {
                $(this).parent('tr').toggleClass('rowclick');
                $(this).parent('tr').siblings().removeClass('rowclick');
        });
    }
)

rowclick is a css class here:

table.tablesorter tbody tr.rowclick td {
    background-color: #8dbdd8;
}

I have tried adding the following to my javascript function that adds a new row:

var createClickHandler = 
        function(newrow) {
            return function(event) {
                //alert(newrow.cells[0].childNodes[0].data);
                newrow.toggleClass('rowclick');
                newrow.siblings().removeClass('rowclick');
            };
        }
row.onclick = createClickHandler(row);

The alert correctly displays the text in the first column of the row when I click the new row. However, my new rows do not respond to the css class. Anyone have any ideas? Thanks in advance.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery