Simulate a key in a field

Robert9

Well-known member
Here what I have:

There is an input field and a button.
I click on the button, and a text "example" is copied to field.

[example]

I also gave the focus to that field.
And now I have to press [space] to have a key-event on my field (then something else happens)
But I am lazy, I want to have this key-event also by script:

Code:
    var e = jQuery.Event("keydown");
     e.which = 32;
     e.ctrlkey = false;
     $(document).trigger(e);

This does not work.

Any idea, what I can do instead?
 
Finally solved with:

Code:
function setFieldInputByButton($myText)
{
    $('#myField').focus();
    $('#myField').val($myText);
    $('#myField').keyup();
}
 
You could save yourself future work/headache by not using jQuery.

 
Top Bottom