jqueryreplacekeyup

Replace dots with commas onkeyup with JQuery


I need to replace dot characters entered by the user with commas. I wrote this code, but it doesn't give the desired 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(".", ",");