2

I managed to catch the Enter keycode and prevent my page from Posting. But I need to add another character in the place of Enter: keycode 39 to be exact.

How can I achieve this? This is my javascript. I see the alert firing but not the following function :(

Enter code

function CkKeyPress(e) {
    var evt = (e) ? e : window.event;
    var key = (evt.keyCode) ? evt.keyCode : evt.which;
    if (key == 13) {
        CancelDefault(evt);
    }
}

function CancelDefault(e) {
    if (e.preventDefault) {
        e.preventDefault();
        alert('enter caught');
        ReplaceEnterWithCharacter();
    }
    e.returnValue = false;
}

function ReplaceEnterWithCharacter() //this function doesnt work somehow
{
    e = $.Event('keyup');
    e.keyCode = 65; // A   
    $('input').trigger(e);
}

<asp:TextBox ID="txtSearch" runat="server" onkeypress="return CkKeyPress(event);"></asp:TextBox>
PiLHA
  • 2,326
  • 1
  • 24
  • 32
wallace740
  • 1,380
  • 5
  • 19
  • 36

1 Answers1

1

I've had some success with this kind of thing:

 jQuery.event.trigger({ type : 'keypress', which : 65 });

Which triggers the keypress event at the current focus.

I don't think it's possible to change the value of the keycode on an event in mid-trigger, nor is it possible to trigger a keypress event on an arbitrary element even if it is an input or is actually the current focus.

James Gaunt
  • 14,631
  • 2
  • 39
  • 57