Solved

Jquery add function result to all table rows

Posted on 2013-01-23
2
421 Views
Last Modified: 2013-01-23
Hello Experts.

I need to add thousand separator to each table row, I made a simple example to illustrate the issue.

The problem here is that the function replaces all the tr's with the first value, what am I doing wrong?

<html>
<head>
    <title>test milhares</title>

    <script src="js/jquery-1.8.3.min.js"></script>




    <script type="text/javascript">

function numberWithSeparation(x) {
    return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ".");
}




$(document).ready(function() {




    var trs = $('tr.ai');


   
        trs.html( numberWithSeparation(trs.html()) );

});




    </script>







</head>
<body>

<table border=1>
    <tr class='ai'><td>50000</td></tr>
    <tr class='ai'><td>80001</td></tr>
</table>



</body>
</html>

Open in new window

0
Comment
Question by:joao_c
2 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 38812512
check here : http://jsfiddle.net/DMnxK/1/
function numberWithSeparation(x) {
    return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ".");
}

$(document).ready(function() {
    $('tr.ai td').each(function() { 
        $(this).text( numberWithSeparation( $(this).text() ) );
    });
});

Open in new window

0
 

Author Closing Comment

by:joao_c
ID: 38812519
Perfect. Thanks a lot.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Illegal invocation error when listener added to Event Target 3 27
WordPress Themes 10 34
Why is this panel heading creating extra space 2 35
Polygon I google maps 1 14
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…

910 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now