I use Robin Herbot inputmask plugin for currency inputs. I want to change the currency symbol each time when the exchange dropdown change event triggers. But the inputmask prefix prevents to change the currency symbol. I have this code:
HTML:
@Html.DropDownListFor(model => model.exchange,
new SelectList(@ViewBag.rates, "Value", "Text",4), null, new
{
@Style = "height:34px;width:370px !important;font-size: 14px;",
@class = "form-control input-lg"
}
)
<input type="text"
class="form-control text-left monerate"
id="price1"
name="price1"
placeholder="₺ 0.00"
data-inputmask="'alias': 'numeric', 'groupSeparator': ',', 'autoGroup': true, 'digits': 2, 'digitsOptional': false, 'prefix': '₺ ', 'placeholder': '0'" />
JQuery:
$('input.monerate').inputmask();
$(document.body).delegate('#exchange', 'change', function () {
exchangeID = $('#exchange :selected').val();
if (exchangeID == 1) {
$("#price1").inputmask({ alias: "currency", prefix: '$ ' });
}
else if (exchangeID == 2) {
$("#price1").inputmask({ alias: "currency", prefix: '€ ' });
}
else if (exchangeID == 3) {
$("#price1").inputmask({ alias: "currency", prefix: '£ ' });
}
else if (exchangeID == 4) {
$("#price1").inputmask({ alias: "currency", prefix: '₺ ' });
}
});
Are there a way to change the prefix?
I've read this answer but did not work for me:
Change the currency symbol or remove it in the inputmask currency
You need to use your
on-changefunction like this below. Also, thevalueof a select by default isstringbut you are checking thevalueasintegerwhich is one reason your code did not work.Add treat the option as integar we can use unary operator (+) - adding a plus sign before getting the value will make it integar
Secondly, for Robin Herbot
inputMaskto load dynamically and changecurrencyprefix from thedropdownyou need to assign theinitialprefix injQueryas well. Since you have defined in statically in theHTMLit will not work as intended.Lastly, your code is all fixed and working as intended with currency
prefixchanging on selection.Live Working Demo: