?
Solved

Jquery add function result to all table rows

Posted on 2013-01-23
2
Medium Priority
?
431 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 2000 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

Stack Overflow Podcast - Developer Story

Welcome to the Stack Overflow podcast recorded Thursday July 20 at Stack Overflow Headquearters in NYC. Your hosts today are podcast regulars Jay Hanlon, David Fullerton, and Ilana Yitzhaki, plus the quite irregular Matt Sherman (Stack Overflow Engineering Manager extraordinaire)

Question has a verified solution.

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

Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
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.…

719 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