javascript - In jquery when you type letter middle of word goes last -
i have 2 inputs when type upper 1 writes bottom 1 same time. have letter i
, different letter ı
in english have same capital letter i
wrote following code address this:
$(".buyuk").on("keypress", function(event) { event.preventdefault(); if (event.which == 105) $(this).val($(this).val() + "İ"); else $(this).val($(this).val() + string.fromcharcode(event.which).touppercase()); });
after write word when want edit word middle adds letter end. example, typed moitor
when though expected output monitor
, became moitorn
instead.
try this:
$(".buyuk").on("keypress", function(event) { event.preventdefault(); var char = string.fromcharcode(event.which).touppercase(); if (event.which == 105) char = "İ"; var og = $(this).val(); var selection = this.selectionstart; $(this).val(og.substr(0, selection) + char + og.substr(this.selectionend, og.length)); this.selectionstart = this.selectionend = selection + 1; });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" class="buyuk">
this works select replacement, , doesn't move cursor position around. if type 12345
, select 34
, type j
, become 12j5
, normal text field. keeps cursor position, if starting typing in middle of input, doesn't move cursor end of text field.
Comments
Post a Comment