Link to home
Start Free TrialLog in
Avatar of Kyle Hamilton
Kyle HamiltonFlag for United States of America

asked on

replace text using regexp on load (add commas to numbers)

Hi,

Any ideas how to add commas to the numbers on page load?

This function works on click but not on load.

 
$(".houseprice").bind('load click',
    function(){
        $(this).text($(this).text().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,"));
    });

Open in new window


Thanks,
Kyle
Avatar of leakim971
leakim971
Flag of Guadeloupe image

$(document).ready(function() {

var txt = $(".houseprice").text();
$(".houseprice").text(t.replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,"));

$(".houseprice").bind('click',
    function(){
        $(this).text($(this).text().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,"));
    });

})
Avatar of Kyle Hamilton

ASKER

hey leakim971,

that almost did the trick. However, I have multiple house prices on the page, so the result is the combination of all the house prices:

so all the houses now cost:

$12,345,614,600,043,456,443,456,412,340

:)

Thanks,
Kyle
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Now why didn't I think of that?

Thank you as always :))
you're welcome, thanks
see you soon