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
LVL 25
Kyle HamiltonData ScientistAsked:
Who is Participating?
 
leakim971Connect With a Mentor PluritechnicianCommented:
$(document).ready(function() {

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

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

})
0
 
leakim971PluritechnicianCommented:
$(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,"));
    });

})
0
 
Kyle HamiltonData ScientistAuthor Commented:
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
0
 
Kyle HamiltonData ScientistAuthor Commented:
Now why didn't I think of that?

Thank you as always :))
0
 
leakim971PluritechnicianCommented:
you're welcome, thanks
see you soon
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.