Reputation: 5992
I need to replace dot character on entered by the user by the comma . I wrote this code but it doesn't give the hopped result
$(".dot").keyup(function (event) {
val = $(this).val();
length = val.length;
if (event.key == '.') {
event.stopPropagation();
$(this).val(val.substring(0, length)+",");
}
});
Upvotes: 0
Views: 2090
Reputation: 1247
have you tried using the string method replace?
var res = str.replace(".", ",");
Upvotes: 3
Reputation: 119
Please try this.
$('input[type = "text"]').on("keyup", function (e) {
var val = $(this).val();
var str=val.replace('.',',');
$(this).val(str);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='text' id='txtVal' />
Upvotes: 3