0

I have more than two fields that is related to currency and I need to format it.I have written the format function already but as of now I can pass only one field/ID i.e. Income to the JS function. How can I pass multiple ID's to this common function so that I can format my fields using this common function.

I want to pass income and trade value that I'm getting through document.getElementById to formatCurrency function? How to pass multiple ID's to a function and set this to the ID? I tried setting the below way

 $("#Income").val(formatCurrency($("#Income").val())); 

but didnt work

function formatCurrency(amt){
  amt = amt.replace("$", "");

    if(amt &&  amt.split(".").length <= 2)
    {
        var formatter = new Intl.NumberFormat('en-US', 
        {
          style: 'currency',
          currency: 'USD',
          minimumFractionDigits: 2
        });
        amt = formatter.format(amt);
    }
     document.getElementById("Income").innerHTML=amt;
     //document.getElementById("Trade").innerHTML=amt;
 }
    formatCurrency(document.getElementById("Income").innerHTML);
//formatCurrency(document.getElementById("Trade").innerHTML);

If Income=8000 output should be $8000.00 and If Trade=900 output should be $900.00 I am able to achieve only for Income and not for trade as I am unable to pass Trade to the formatCurrency.

3
  • Did you try to use classes insted of ids? Commented Jan 11, 2019 at 18:35
  • use commas api.jquery.com/multiple-selector Commented Jan 11, 2019 at 18:36
  • So pass in the selector and use that. formatCurrency("Income") and inside the method read the text and output it Commented Jan 11, 2019 at 19:08

2 Answers 2

1

So just read the text and output it in the function. Pass up the selector.

function formatCurrency(selector) {
  var elem = document.querySelector(selector)
  var amt = elem.textContent.replace("$", "");
  if (amt && amt.split(".").length <= 2) {
    var formatter = new Intl.NumberFormat('en-US', {
      style: 'currency',
      currency: 'USD',
      minimumFractionDigits: 2
    });
    amt = formatter.format(amt);
  }
  elem.textContent = amt;
}

formatCurrency("#one")
formatCurrency("#two")
<div id="one">$.99</div>
<div id="two">$1200.00</div>

If you want to make it so your selector can match more than one thing, that it is querySelectorAll with a loop

function formatCurrency(selector) {
  document.querySelectorAll(selector).forEach(function (elem) {
    var amt = elem.textContent.replace("$", "");
    if (amt && amt.split(".").length <= 2) {
      var formatter = new Intl.NumberFormat('en-US', {
        style: 'currency',
        currency: 'USD',
        minimumFractionDigits: 2
      });
      amt = formatter.format(amt);
    }
    elem.textContent = amt;
  })
}

formatCurrency(".money")
<div class="money">$.99</div>
<div class="money">$1200.00</div>

Sign up to request clarification or add additional context in comments.

Comments

0

Here is a dummy function thats can help you to send any number of arguments to a function

function multipleArgs(){
     for (var i=0; i < arguments.length; i++)
      console.log(arguments[i])
   }; 
// Call above function
multipleArgs(1,2)

But for your problem, you can pass an object to the function like

{type:"trade",value:$("#Trade").val()} 
{type:"Income",value:$("#Income").val()} 

and modify your common function

function formatCurrency(amt){
    switch(amt.type){
     "Income":
            // do Income related work
            break;
    "Trade":
            //do Trade related work
            break;
    default: break;

    }

 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.