I have a jquery function that detects and formats currency if datatype of an input field is set to currency. how do i make this function not run if another select field value is set to BTC here are codes. JS
$("input[data-type='currency']").on({
keyup: function() {
formatCurrency($(this));
},
focusout: function() {
formatCurrency($(this), "blur");
}
});
and here is my formatCurrency Function
function formatNumber(n) {
return n.replace(/\D/g, "").replace(/\B(?=(\d{3})+(?!\d))/g, ",")
} // format number 1000000 to 1,234,567
function formatCurrency(input, blur) {
var input_currency = ""; //the currency symbol that shows beofore the amount
var input_val = input.val();
if (input_val === "") { return; }
var original_len = input_val.length;
var caret_pos = input.prop("selectionStart");
if (input_val.indexOf(".") >= 0) {
var decimal_pos = input_val.indexOf(".");
var left_side = input_val.substring(0, decimal_pos);
var right_side = input_val.substring(decimal_pos);
left_side = formatNumber(left_side);
right_side = formatNumber(right_side);
if (blur === "blur") {
/* right_side += "00"; */
}
right_side = right_side.substring(0, 2);
input_val = input_currency + left_side + "." + right_side;
} else {
input_val = formatNumber(input_val);
input_val = input_currency + input_val;
if (blur === "blur") {
input_val += ".00";
}
}
input.val(input_val);
HTML
<input data-type="currency" name="amount">
<select class="" name="currency" id="currency" >
<option value="$" selected>USD</option>
<option value="₿">Bitcoin</option>
</select>