Solved

Jquery add function result to all table rows

Posted on 2013-01-23
2
419 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
Comment Utility
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
Comment Utility
Perfect. Thanks a lot.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
In this tutorial viewers will learn how add a full-size background image to a webpage using CSS3. Create a new HTML document with an internal stylesheet.: In CSS, define the html element to have a background image. Use a high resolution image.: In t…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

771 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

15 Experts available now in Live!

Get 1:1 Help Now