John John - 7 months ago 23
Javascript Question

Keyup event prevents arrow keys in text field in Chrome

Please check this on Google Chrome browser:



jQuery('#tien_cong').keyup(function(e) {
jQuery(this).val(jQuery(this).val().replace(".", ","));
var sum = 0;

var tien_cong = jQuery('#tien_cong').val();
tien_cong = tien_cong.replace(/,/g, '');
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class="textfield" id="tien_cong" type="text" data-required="" data-type="text" name="tien_cong" placeholder="" value="" size="">





I try to replace
.
by
,
when user types somethings with
.
in a textbox.

On Chrome browser, when user press left cursor button on keyboard , it can not move.

enter image description here

Why?

Answer

Right now the input is updating every time a key is pressed. Testing to see if the character is a '.' before replacing will prevent the script from running when it doesn't need to, and prevents the cursor from resetting.

jQuery('#tien_cong').keyup(function(e) {
  if(e.which === 190) {
    jQuery(this).val(jQuery(this).val().replace(".", ","));
  }     
 
  var sum = 0;

  var tien_cong = jQuery('#tien_cong').val();
  tien_cong = tien_cong.replace(/,/g, '');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class="textfield" id="tien_cong" type="text" data-required="" data-type="text" name="tien_cong" placeholder="" value="" size="">

Comments