Rafael
Rafael

Reputation: 1223

Loop through all cells in table row using jQuery

What would be the best way to achieve this using jQuery, I'm trying to change the text for each cell upon row click, but I can't access the cell text in the first place (str is empty):

$("tr").bind("click", function() {
    $cells = $(this).children();
    $cells.each(function(index, cell) {
        var str = $(cell).val();
    });
});

Upvotes: 2

Views: 3438

Answers (1)

martynas
martynas

Reputation: 12290

A td does not have a value. Use .text() instead:

$("tr").bind("click", function() {
    $cells = $(this).children();
    $cells.each(function(cell) {
        var str = cell.text();
    });
});

Upvotes: 1

Related Questions