Phi Huynh
Phi Huynh

Reputation: 197

jQuery: Change keyboard value when input

Example: when I press key "A" on a keyboard, then the value will change to "S" and text box show up "S".

How can I do this with jQuery or Tinymce 4?

Thank you very much!

Upvotes: 0

Views: 2060

Answers (1)

currarpickt
currarpickt

Reputation: 2302

You will need to check the keyCode in every keypress event. If the keyCode is the same with A then you need to change it to whatever string you want, for example: S.

<div>
    <label>Test:</label>
    <input type="text" id="test">
</div>

$("#test").on("keypress", function (e) {
    if (97 == e.keyCode || 65 == e.keyCode) {
        e.preventDefault();
        var newString = $("#test").val() + "S";
        $("#test").val(newString);
    }
});

97 is the keycode for a and 65 is keycode for A.

a will not be shown in the box with e.preventDefault();

Fiddle is here.

Upvotes: 3

Related Questions