views:

69

answers:

4

Hi there, i have a table(s) with different dynamic-generated content inside. How can i do that all with only numbers inside will have text-align to the right?

Well, i know how to add css to some element, the question is how to get only tds with digits

Thank you!

A: 

Your question is not real clear but you can align-right in any cell like this:

<td align="right">123</td>
Scott Evernden
He needs a jQuery selector to target table cells containing only numbers.
Alec
The way I read it -and I'm taking a huge intuitive leap- is that the data *in* the cells is dynamically generated. So he needs JS/jQuery to find those cells with only numbers and then `text-align: right` them. But I'm mostly guessing, unfortunately.
David Thomas
the problem is, that i dont know if the cell contains only numbers, thats why i need js/jQuery solution
M2_
A: 

You can use the filter option:

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

This will match "123", "12 3", "12.3" and "12,3" but not "abc", "abc123", etc.

Alec
That works great, thank you very much!
M2_
and what changes should be if there is ` ` symbol instead of spacebar?
M2_
I'm not sure if you can match either `\s` or ` ` in this way (without also match e.g. "12nb"), but this will work: `this.innerHTML.replace(' ','').match(/^[0-9\s\.,]+$/);`
Alec
Thank you very much again, it worked
M2_
A: 

Hi, I'd suggest some jquery, I've mocked up on jsbin.

$(document).ready(function(){ $("td").each(function(index, item){
if(isNaN($(item).text()) == false) { $(item).addClass("match"); } }); });

(Sorry, my stackoverflow formatting skills are a little weak.)

J Pollock
Then you might want to head over to [the Editing Help](http://stackoverflow.com/editing-help) to have a quick look-see =)
David Thomas
+2  A: 

If you want a jQuery-ish solution, one way is to use a custom selector:

$.expr[':'].isNumeric = function(obj){
  return !isNAN( $(obj).text() );
};

And then:

$('td:isNumeric').addClass('numeric');

Which assumes the style:

td.numeric { text-align: right; }

...or if you prefer:

$('td:isNumeric').css('text-align','right');
Ken Redler