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> THB</label>
<label>
<input type="radio" name="prop_crnt" class="currency" value="1" id="crnt-usd"> 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/