Trigger Input Events in JavaScript/jQuery
Q: How do you simulate a user typing text in an input box, triggering event handlers, without adding actual text?
A: Direct calls to specific events can be made:
$(function() { $('item').keydown(); $('item').keypress(); $('item').keyup(); $('item').blur(); });
For completeness, consider triggering .focus() and .change(). For simulating key-events with specific keys, use:
$(function() { var e = $.Event('keypress'); e.which = 65; // Character 'A' $('item').trigger(e); });
For browser compatibility with the .which property, refer to the resource: [jQuery Event Keypress: Which key was pressed?](https://stackoverflow.com/questions/12195223/jquery-event-keypress-which-key-was-pressed).
The above is the detailed content of How Can I Programmatically Trigger Input Events in JavaScript/jQuery Without User Interaction?. For more information, please follow other related articles on the PHP Chinese website!