I tried to build a plugin that would replace all numbers in td's of a
certain class with numbers formatted with commas.

For instance:

<td>1111</td><td>2222</td>

Would be replaced with

<td>1,111</td><td>2,222</td>

Here's the code... doesn't work for phooey.

When I debug in firebug, the variable 'number' equals (literally):
"function (value) {\n return value == undefined ? this.length ?
this[0].innerHTML : null : this.empty().append(value);\n}"

$(".commas").addCommas();

jQuery.fn.addCommas = function() {
    number = '' + this.html;
    if (number.length > 3) {
    var mod = number.length % 3;
    var output = (mod > 0 ? (number.substring(0,mod)) : '');
    for (i=0 ; i < Math.floor(number.length / 3); i++) {
    if ((mod == 0) && (i == 0))
    output += number.substring(mod+ 3 * i, mod + 3 * i + 3);
    else
    output+= ',' + number.substring(mod + 3 * i, mod + 3 * i + 3);
    }
    return (output);
    }
    else return number;
};

Reply via email to