Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace input in real time with jQuery

Tags:

jquery

I have a problem with this script, I want to replace anything entered in the input field with a specific letter in real time, with Hello World in the field.

 <input id="inputID" type="text" value="" maxlength="11"/>
 $('#inputID').keyup(function(e){
  var cv=["h","e","l","l","o"," ","w","o","r","l","d",""];

  var i=$('#inputID').val();

  for (j=0;j<11;j++){

    this.value = this.value.replace(i[j],cv[j]);

}

});

This script works well when I write slowly but not when I write quickly. thanks for your help

like image 728
Jamal.Eddi Avatar asked Jan 23 '26 06:01

Jamal.Eddi


1 Answers

Try this way:

$('#inputID').keyup(function(e){
     var cv = 'hello world';
     this.value = cv.substr(0, this.value.length);
});

See the working demo.

like image 102
xdazz Avatar answered Jan 24 '26 23:01

xdazz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!