I want to add 'a' to the value of input when click a button
Here is my code(with jQuery 1.4.4):
$("#button").click(function(){
$("#input").trigger("focus");
var e = jQuery.Event("keypress");
e.which = '97';
$("#input").trigger(e);
})
However, it seems only to trigger 'focus' event ,but failed to 'keypress'.
According to the documentation
so the best you could do is
I used:
I have known how to deal with it.
Add a eventListener on keypress event to the input and use val to change the value.
In this way there is no need to trigger focus event.
In case you need to take into account the current cursor and text selection...
This wasn't working for me for an AngularJS app on Chrome. Someone pointed out the trigger event will not make the character visible in the input field (at least, that's what I was seeing). In addition, the previous solutions don't take into account the current cursor position and text selection in the input field. I had to use a wonderful library jquery-selection.
I have a custom on-screen numeric keypad that fills in multiple input fields. I had to...
On blur, save the current text selection (start and stop)
When a button on the my keypad is pressed:
you don't need keypress or any other event of input just use
val
.. and focus it...try this
fiddle here
like this?? Sorry, I'm confused with your writings..
reference: .val(function(index, value));