R Clowes
R Clowes

Reputation: 69

jQuery change css right align when td data numeric (dynamic table)

$('tr > td:gt(0)').filter(function() {
                        return this.innerHTML.match(/^[0-9\s\.,]+$/);
                        }).css('text-align','right');

I am trying to loop through a dynamic table and right align every td that contains numeric data on each row EXCEPT the first td on each row.

It works on the first tr but not each subsequent row...

Upvotes: 1

Views: 2073

Answers (1)

Michael
Michael

Reputation: 1467

The reason this isn't working is that your jQuery selector is collecting all td elements (that are children of trs), and then sub-selecting all but the first one. To avoid this, you can iterate over the rows of the table, applying your filter to the td elements of each one:

$('tr').each( function () { 
    $(this).children('td:gt(0)').filter(function() {
        return this.innerHTML.match(/^[0-9\s\.,]+$/);
    }).css('text-align','right')
});

If you only want to apply this to a certain table, change the selector in the first line to '#table-id tr'.

Here it is in action: http://jsfiddle.net/munderwood/d48Q4/1/

Upvotes: 1

Related Questions