priya
priya

Reputation: 41

How to pass multiple Jquery ID's to one function?

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.

Upvotes: 0

Views: 206

Answers (2)

Kumar Nitesh
Kumar Nitesh

Reputation: 1652

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;

    }

 }

Upvotes: 0

epascarello
epascarello

Reputation: 207501

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>

Upvotes: 1

Related Questions