Search code examples
jquerykeyup

Replace dot character once entered by a comma in jquery


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)+",");
     }
 });

Solution

  • have you tried using the string method replace?

    var res = str.replace(".", ",");