3

In following example I am attempting to change the charCode of the key pressed but it does not change. When I press "a" I want it to type "b". What am I doing wrong?

$("#target").keypress(function(event) {
     if ( event.which == 97 ) {
         //alert('pressed a');
         //event.preventDefault();
         event.keyCode = 98;
         event.charCode = 98;
         event.which = 98;
     }
});
gdoron
  • 147,333
  • 58
  • 291
  • 367
DavidW
  • 5,069
  • 14
  • 46
  • 68
  • 2
    I'm not sure you *can* override the character code like that. Do you have a reference stating this is possible? – Pekka Feb 19 '12 at 09:34
  • @Pekka. It can be done this way: – gdoron Feb 19 '12 at 09:45
  • @gdoron yeah, saw it! That's a fine workaround. (You can't *generally* modify the value and pass it further down the event chain though, that's what I meant.) – Pekka Feb 19 '12 at 09:46
  • @Pekka. Yep... And I upvoted your comment... =) – gdoron Feb 19 '12 at 09:48
  • @Pekka the problem with gdorons approach is that it will not work if cursor is in the middle of text, or if something is selected. – DavidW Feb 19 '12 at 10:45

2 Answers2

4

You can't override the keycode in the event object...

Look at this snippet:

$('#target').keypress(function(e){
    if (e.which == 97)
        this.value = this.value + String.fromCharCode(98)
    else
        this.value = this.value + String.fromCharCode(e.which)

    ....

    return false;
})
gdoron
  • 147,333
  • 58
  • 291
  • 367
  • Does that mean to do this (that OP asked) I can replace the input with new character and can trigger the event again ? – The Alpha Feb 19 '12 at 09:50
  • @Heera. I didn't fully understand what you meant, but it won't trigger the event again. – gdoron Feb 19 '12 at 09:52
  • What if I want to trigger the event with character b when user inputs a ? – The Alpha Feb 19 '12 at 09:54
  • @Heera. You can do a workaround with `$(this).keypress()` But **WHY* would you need to do so? – gdoron Feb 19 '12 at 09:56
  • Thanks, just curiosity, what's wrong with learning new things, mind is always hungry, don't want to mis anything ! – The Alpha Feb 19 '12 at 09:58
  • 3
    The problem with this is that it will not work if cursor is in the middle of text, or if something is selected. – DavidW Feb 19 '12 at 10:43
  • Looks like this will have to do. I will aditionally use http://stackoverflow.com/a/2819568/720943 for inserting at selection or cursor location. – DavidW Feb 19 '12 at 11:08
0

replace comma and dash to slash.

$('.pdate').on('keypress', function (e) {
        var ch = String.fromCharCode(e.keyCode);
        $("div").text(e.keyCode)
        if (!((ch >= '0' && ch <= '9') || 
        ch == '/' || ch == ',' || ch == '-')) {
            return false;
        }
        if (ch == ',' || ch == '-')
        {
            var val = $(this).val();
            var s = this.selectionStart;
            val = val.slice(0, s) + "/" + val.slice(s, val.length);
            $(this).val(val)
            this.selectionStart = s +1;
            this.selectionEnd = s +1;
            return false;
        }
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input type="text" class="pdate" />
<div></div>
mehdi
  • 645
  • 1
  • 9
  • 9