-3

I wrote this code to replace some chars in a string:

$(".rtl:not(.num)").keypress(function(e)
 { var key = (e.keyCode || e.which);
   var vlu = $(this).val();
   var charTyped = String.fromCharCode(key);

   if (charTyped=='ك') 
    { vlu.replace(/ك/g,'ک'); 
      alert("keh"); }

   if (charTyped=='ي') 
    { vlu.replace(/ي/g,'ی'); 
      alert("yeh"); }

   alert(vlu); 
 });

After the code executes, vlu has not changed. What is wrong?

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
APH
  • 29
  • 7

1 Answers1

1

Replace does not change the original string, it returns a new string.

MDN String replace()

var str = "abc123";
var updated = str.replace("123","");
console.log("str: ", str);
console.log("updated: ", updated);
epascarello
  • 204,599
  • 20
  • 195
  • 236
  • I edit my code as your suggestion. but it's not work. – APH May 16 '15 at 13:45
  • @user3801352: please don't incorporate the changes in your question. In invalidates existing answers. If you have problems applying the solution, add detailed information. 'It doesn't work' doesn't contain useful information. – Felix Kling May 16 '15 at 14:15