Solved

Jquery add function result to all table rows

Posted on 2013-01-23
2
428 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
This article discusses four methods for overlaying images in a container on a web page
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

821 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