Let's say there is textarea with already present text for example: AA. How we can detect the pressed key and change only this character with something else. Now if A key is pressed through keyboard text should become AAB already existing A characters are unaltered but newly pressed A key should write B in textarea.
The code I have is
<textarea>aa</textarea>
<script>
jQuery('textarea').keyup(function (e) {
//THIS REPLACES ALL THE TEXT NOT JUST THE PRESSED CHARACTER
//Need code that should replace just this pressed key not all text of textarea
jQuery('textarea').val(jQuery('textarea').val().replace(/a/g, "b"));
});
</script>
Ideal solution will select character from pressed key e
and replace it, but if not possible then maybe replacing last character in textarea can be a poor workaround.
Also ok if keypress
, keydown
or similar event is used.