[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2011-10-06
5
Medium Priority
?
331 Views
Last Modified: 2012-08-14
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
0
Comment
Question by:Kyle Hamilton
  • 3
  • 2
5 Comments
 
LVL 83

Expert Comment

by:leakim971
ID: 36927938
$(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
 
LVL 25

Author Comment

by:Kyle Hamilton
ID: 36928068
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
 
LVL 83

Accepted Solution

by:
leakim971 earned 2000 total points
ID: 36928108
$(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
 
LVL 25

Author Closing Comment

by:Kyle Hamilton
ID: 36928124
Now why didn't I think of that?

Thank you as always :))
0
 
LVL 83

Expert Comment

by:leakim971
ID: 36928138
you're welcome, thanks
see you soon
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.
Suggested Courses
Course of the Month18 days, 17 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question