32

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".

2
  • As per the trigger doc , it does not perfectly replicate a naturally-occurring event. Commented Mar 21, 2013 at 11:00
  • Works for me: jsfiddle.net/2YuN5/1 ...ah wait, I'm using jQuery 1.9... Time for you to do an update. Commented Mar 21, 2013 at 11:00

8 Answers 8

57

I used the trigger method:

$('#selector').val(quantity).trigger("input");
Sign up to request clarification or add additional context in comments.

4 Comments

The val() and trigger() methods are chained to the selector.
My mistake. I didn't realize .val(quantity) was chained unlike an empty .val().
You can also use .trigger('change') if you're listening or a change vs. input.
hm, trigger('input') didn't work for me. but element.dispatchEvent(new InputEvent('input')) worked perfectly
15

like this?? Sorry, I'm confused with your writings..

$("#button").click(function(){
    $("#input").trigger("keypress") // you can trigger keypress like this if you need to..
    .val(function(i,val){return val + 'a';});
});

reference: .val(function(index, value));

Comments

4

Why not just append to val()?

$("#button").click(function(){
  $("#input").val(
    $("#input").val() + "a"
  );
})

1 Comment

Because I want to write a demo like fancyInput. And the origin event should be another keypress.Here i use click to simplify the code.
3

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.

$("#button").click(function(){
    var e = jQuery.Event("keypress");
    e.chara = 'a';
    $("#input").trigger(e);
});

$("#input").keypress(function(e){
    $(this).val(e.chara);
})

Comments

1

According to the documentation

Although .trigger() simulates an event activation, complete with a synthesized event object, it does not perfectly replicate a naturally-occurring event.

so the best you could do is

$("#button").click(function(){
    $("#input").trigger("focus").val($("#input").val() + 'a');
})

Comments

0

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...

  1. On focus, save the lastFocus.element
  2. On blur, save the current text selection (start and stop)

    var pos = element.selection('getPos')
    lastFocus.pos = { start: pos.start, end: pos.end}
    
  3. When a button on the my keypad is pressed:

    lastFocus.element.selection( 'setPos', lastFocus.pos)
    lastFocus.element.selection( 'replace', {text: myKeyPadChar, caret: 'end'})
    

Comments

0

Try this :

$("#triggerbtn").click(function(){
$("#InputField").trigger("keypress")
.val(function(i,val){
return val + 'j';
});
});

Comments

-1

you don't need keypress or any other event of input just use val.. and focus it...

try this

 $("#button").click(function(){

   $("#input").val('a').focus();
})

fiddle here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.