0

I need to change the prefix of the currency in the input text field. So the currency prefix will change according to the radio. I can make the input field reset when a radio is clicked but cannot change the prefix.

Here is my code :

 Price (
<label>
  <input type="radio" name="prop_crnt" class="currency" value="0" id="crnt-thb" required>&nbsp;THB</label>
<label>
  <input type="radio" name="prop_crnt" class="currency" value="1" id="crnt-usd">&nbsp;USD</label>):

Javascript :

//reset input on radio clicked
$("input[name='prop_crnt']").on('click', function() {
  $('input.money').val('');
  //change currency prefix
  if ($('#crnt-thb').val()=="0") {
    var crncy="฿";
  }else{
    var crncy="$";
  }
});



var format = function(num) {
  var str = num.toString().replace(crncy, ""),
    parts = false,
    output = [],
    i = 1,
    formatted = null;
  if (str.indexOf(".") > 0) {
    parts = str.split(".");
    str = parts[0];
  }
  str = str.split("").reverse();
  for (var j = 0, len = str.length; j < len; j++) {
    if (str[j] != ",") {
      output.push(str[j]);
      if (i % 3 == 0 && j < (len - 1)) {
        output.push(",");
      }
      i++;
    }
  }
  formatted = output.reverse().join("");
  return (crncy + formatted + ((parts) ? "." + parts[1].substr(0, 2) : ""));
};
//currency
$(".money").keyup(function(e) {
  $(this).val(format($(this).val()));
});

Here is jfiddle : https://jsfiddle.net/u0rc9trh/2/

Wilf
  • 2,297
  • 5
  • 38
  • 82
  • `crncy` is not defined within the format function. Perhaps you mean to make it a global? – Matt Feb 05 '16 at 03:53
  • 2
    Won't this condition `if ($('#crnt-thb').val()=="0")` always be true? You need to test whether that radio button is checked, not what its value is. Or test the value of the clicked radio using `this`. – nnnnnn Feb 05 '16 at 03:53
  • 2
    ^^^^ and one more issue. Check [Demo](https://jsfiddle.net/tusharj/u0rc9trh/3/) – Tushar Feb 05 '16 at 03:53
  • Tushar, why sometimes it is `undefined` in there? – Wilf Feb 05 '16 at 03:55
  • Is that my code? Haha, didn't think it was still in use; either way take a look at my original answer and you could use a simple if/else to check for currency: http://stackoverflow.com/questions/23966148/how-can-i-format-currency-by-using-jquery/23966200#23966200 – faino Feb 05 '16 at 03:56
  • @faino, nice to see the author. I cannot remember where I took it from but very thanks for the creation. :D – Wilf Feb 05 '16 at 03:57

1 Answers1

1

Something like this should be what you're looking for, although it is written for your needs so maybe add some variables or something. Whatever, this should work:

var format = function(num){
    var cur = $('#crnt-thb').is(":checked") ? "฿" : "$", str = num.toString().replace("฿", "").replace("$", ""), parts = false, output = [], i = 1, formatted = null;
    if(str.indexOf(".") > 0) {
        parts = str.split(".");
        str = parts[0];
    }
    str = str.split("").reverse();
    for(var j = 0, len = str.length; j < len; j++) {
        if(str[j] != ",") {
            output.push(str[j]);
            if(i%3 == 0 && j < (len - 1)) {
                output.push(",");
            }
            i++;
        }
    }
    formatted = output.reverse().join("").toString();
    return(cur + formatted + ((parts) ? "." + parts[1].substr(0, 2) : ""));
};
//currency
$(".money").keyup(function(e) {
    $(this).val(format($(this).val()));
});
$("input[name='prop_crnt']").on('click', function(){
    $(".money").keyup();
});

As for the double replace(), take a look at this answer if you wish to simplify things. Also, a fiddle for you to test.

P.S. Thanks for using my code, I wrote it to help people and I'm glad it still is. Cheers!

Community
  • 1
  • 1
faino
  • 3,194
  • 15
  • 17