-1

This is a code for key press 'A' (65): See: http://jsfiddle.net/3xTM2/466 The problem is: Why 'A' doesn't display after clicking submit?

var onclick = function(){
    var e = $.Event('keypress');
    e.which = 65; // Character 'A'
    $(this).focus().trigger(e);
    console.log('pressed');
};

$('#test').click(onclick);
<input id="test" type="input" />

I want to simulate 'a' click key at input form after clicking Submit button. Why 'A' hasn't been typed into 'input form'? How to change this code to display 'a' at input form after simulate it by javascript?

riko10
  • 41
  • 1
  • 1
  • 4

2 Answers2

0

You have both in this solution. The keypress and the value.

var onclick = function(){
    var e = $.Event("keypress", { keyCode: 65});
    var c = 'A'; // Character 'A'
    $(this).trigger(e).val(c);
};

$('#test').click(onclick);

JSFIDDLE.

loveNoHate
  • 1,549
  • 13
  • 21
  • You have to use `keyCode: 13`. – loveNoHate Sep 20 '14 at 11:33
  • SO why does it not submiting input form while it simulate Enter press key? After clicking on input form it display "A", not "Enter function" like submit. If you click Enter on keyboard do you see "A" or something like "Submit" ? I want to make it like clicking on keyboard so it mustn't display "A" while clicking input form! – riko10 Sep 21 '14 at 09:42
0

Apologies, this is not an answer but a question, but I dont have permissions to add a comment. The below code works perfectly for me, as in the jsfiddle.

My problem is I am using it to add a 'space' when a user clicks on an auto-populated field. Currently it replaces the auto-populated content. How would I change it so that the auto-populated field is kept, and the space is added after?

var onclick = function(){
    var e = $.Event("keypress", { keyCode: 65});
    var c = 'A'; // Character 'A'
    $(this).trigger(e).val(c);
};

$('#test').click(onclick);

JSFIDDLE.

designlobby
  • 35
  • 1
  • 7