1

I have tried the following in jQuery to trigger a keydown event into an input box:

http://jsfiddle.net/W5fNh/8/

and wonder why no keydown event with A and Enter was triggered. If I do type it in and press Enter myself, the form does get submitted.

The code:

$(function () {

    $("#the-input").trigger("focus");

    $("#the-input").trigger(
        jQuery.Event('keydown', {
            keyCode: 65
    }));

    $("#the-input").trigger(
        jQuery.Event('keydown', {
            keyCode: 13
    }));

    $("#the-input").trigger(
        jQuery.Event('keydown', {
            which: 65
    }));

    $("#the-input").trigger(
        jQuery.Event('keydown', {
            which: 13
    }));

});

with html:

<form id="the-form" action="some.php" method="get">
    <input id="the-input" type="text" name="var">
</form>
nonopolarity
  • 146,324
  • 131
  • 460
  • 740

1 Answers1

0

Ok, it seems like the answer is: jQuery's trigger can cause the event handlers to be called, but not to actually emulate a user typing things in, to appear on the browser.

That is, except for the focus or blur, which actually will show up in the browser. So what will show and what will not, I wonder what it is for each event.

P.S. for example, if it is radio buttons, then in fact they get "clicked": jsfiddle example: http://jsfiddle.net/awbus/2/

nonopolarity
  • 146,324
  • 131
  • 460
  • 740