Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8921731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:37:48+00:00 2026-06-15T06:37:48+00:00

Sorry for the noob question but I am trying to apply this javascript function

  • 0

Sorry for the noob question but I am trying to apply this javascript function to data in an html table. The function comes from this Stack Overflow post.

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

I wish to apply this function to something like this:

<table>
    <tr>
        <td>5000</td>
        <td>5600</td>
    </tr>
</table>

with the hopes of returning 5,000 next to 5,600. I can’t figure out this simple thing. I know I need to use an id or class on one of the tags and that this needs to match something in the function so I tried

<table>
    <tr class="numberWithCommas">
        <td>5000</td>
        <td>5600</td>
    </tr>
</table>

to no avail. I’m sure the function works given the amount of upvotes.

Can a kind soul please walk me through the steps to make this work? I’ve googled all over for a basic tutorial and haven’t found anything.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T06:37:50+00:00Added an answer on June 15, 2026 at 6:37 am

    You first need to get all the tds, call the function on the content and insert the result back into the cell.

    HTML

    <table>
        <tr class="numberWithCommas">
            <td>5000</td>
            <td>5600</td>
        </tr>
    </table>
    

    Javascript

    function numberWithCommas(x) {
        return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    }
    
    var elements = document.querySelectorAll('.numberWithCommas td'),
        i;
    
    for (i in elements) {
        if (elements[i].innerHTML !== undefined) {
            elements[i].innerHTML = numberWithCommas(elements[i].innerHTML);
        }
    }
    

    Demo

    To explain all this code:

    This is your function:

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

    This allows us to get all the td within all trs with numberWithCommas as a class and sets them all as an array in the javascript variable elements:

    var elements = document.querySelectorAll('.numberWithCommas td'),
        i;
    

    We then loop through the array accessing one td at a time:

    for (i in elements) {
        ...
    }
    

    Inside the for loop we check that the value of the cell is not empty:

    if (elements[i].innerHTML !== undefined) {
        ...
    }
    

    Inside the if statement we call the function numberWithCommas() and pass it the value of elements[i].innerHTML. We then get back the resulting string and apply it to elements[i].innerHTML:

    elements[i].innerHTML = numberWithCommas(elements[i].innerHTML);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry this is a noob question but if I only need some initial data
I'm sorry if this comes through as a noob question, but I'm really stuck
Sorry, this might be a basic/stupid/noob question - I am just trying to tweak
This is probably a noob question, but how I've implemented the appendTo() function below
This is a noob question, so I'm sorry if I offend somebody. But how
I'm sorry if this question is noob-ish but I'm not having much luck with
Sorry if this is a noob question but is there a way to combine
Sorry if this is a bit of a noob question but I am still
Hi Sorry for this noob question but here goes...i have a working slidetoggle on
Sorry if this is a noob question, but All my code is error-free and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.