Note that there are some explanatory texts on larger screens.

plurals
  1. POJquery Keyboard Event trigger
    primarykey
    data
    text
    <p>I am trying to simulate a Keyboard event with jquery. What I want is when I click a button, I want a character to appear in a textarea. I need the action to be a keyboard simulation not a simple append. I have tried all the possible solutions on the web without any success, so I would be grateful if anyone can help me.</p> <p>Here is my HTML/JS/Jquery code :</p> <pre><code>&lt;html&gt; &lt;head&gt;&lt;/head&gt; &lt;body&gt; &lt;script type='text/javascript' src='jquery-1.9.1.min.js'&gt;&lt;/script&gt; &lt;input type='text' id="input"&gt;&lt;/input&gt; &lt;script type='text/javascript'&gt; function simulateKeyPress() { document.getElementById("text").focus(); var e = $.Event("keypress"); e.which = 97; $("input").trigger(e); } &lt;/script&gt; &lt;br/&gt; &lt;button id="button" onclick='simulateKeyPress()'&gt;Press Me&lt;/button&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>As you can see, when the button is clicked, I only get a focus on the text element, but no character appears, any ideas?</p> <p>looks like I was not clear enough. Here is another sample, I am using the <a href="http://craig.is/killing/mice" rel="nofollow">MouseTrap</a> library to capture keyboard events. Here is my code.</p> <pre><code>&lt;html&gt; &lt;header&gt; &lt;title&gt;test&lt;/title&gt; &lt;/header&gt; &lt;script type='text/javascript' src='MouseTrap.js'&gt;&lt;/script&gt; &lt;script type='text/javascript' src='jquery-1.9.1.min.js'&gt;&lt;/script&gt; &lt;body&gt; &lt;input type='text' class="mousetrap" id="input"&gt;&lt;/input&gt; &lt;script type='text/javascript'&gt; Mousetrap.bind('4', function(){alert("you pressed 4" );}); function simulateKeyPress(character) { document.getElementById("input").focus(); var input = $('#input'); input.val(input.val() + character); } &lt;/script&gt; &lt;br/&gt; &lt;button type="button" onclick='simulateKeyPress(4)'&gt;Press Me&lt;/button&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>With this code, whenever you press '4' on the keyboard, an alert box will appear. All I want is when I click the button, the same event to be created. Ideas?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload