0

It is possible to program a "Enter" after a value is added to a input text box?

If yes, I need help on my code below. It does't trigger the "ENTER" keypress.

My code as below:

$('#clo_cart').click( function () {

/*Save ENTER in variable*/
var eneterKey = jQuery.Event("keydown");
eneterKey.which = 13; 
eneterKey.keycode = 13;

$("#scan_char").focus().val('CLOSECTN').trigger(eneterKey);
}//end of button click

Pls Help

Mehdi Benmoha
  • 3,694
  • 3
  • 23
  • 43
JSTai
  • 35
  • 1
  • 7
  • I think there are some differences between keydown and keypress in terms of the value assigned to `event.which`. Can you try with `keypress` event? – Nisarg Shah Dec 08 '17 at 09:30
  • 2
    This seems a round-about way of doing things. Why not simply call whatever the 'enter' key would have triggered? – KIKO Software Dec 08 '17 at 09:31
  • @KIKOSoftware Can you me show some example? – JSTai Dec 08 '17 at 09:33
  • @JSTai : what happen if you enter key ? – Niklesh Raut Dec 08 '17 at 09:33
  • @NisargShah Thank bro. It Work!!! – JSTai Dec 08 '17 at 09:34
  • @JSTai assume you have written a 10 lines of code that runs when you press the `Enter` key... what you can do is place this code in a function and then call this function when ever there is a `Enter` key press and also when ever you want to trigger the `Enter` key press.. hope this make sense – Rajshekar Reddy Dec 08 '17 at 09:35
  • @user2486 After "Enter" trigger, my logic is to process or save data to DB. The concept is like "Checkout" after some add to cart. – JSTai Dec 08 '17 at 09:38
  • 2
    This is guesswork. If it is a form, 'enter' would quite often trigger a form submission. So I would submit the from instead of emulating an 'enter' key stroke. – KIKO Software Dec 08 '17 at 09:38
  • @RajshekarReddy Agree. I post this question because my code didt work. – JSTai Dec 08 '17 at 09:38
  • @JSTai : you just need to submit form , google for it not trigger enter. – Niklesh Raut Dec 08 '17 at 09:44
  • @user2486 You are right too, however I would like the user to checkout via scanning a "CHECKOUT "barcode. Put it simple to kill 2 bird in 1 stone. – JSTai Dec 08 '17 at 09:47
  • @JSTai : dont use stone , its 21st century, blast the world. `No code`, `No Question`. you can add `form submit code by jquery` after `$("#scan_char").focus().val('CLOSECTN')` – Niklesh Raut Dec 08 '17 at 10:01

3 Answers3

0

Just replace the keydown event with keypress event. There are some differences between how they use event.which property. This might be one of the cases where the two of them use different values for event.which.

var eneterKey = jQuery.Event("keypress");

Also note that which property has been deprecated. You can compare with the event.key property. It should have value Enter when the enter key is to be simulated.

$('#clo_cart').click( function () {

    /*Save ENTER in variable*/
    var eneterKey = jQuery.Event("keydown");
    eneterKey.key = 'Enter';

    $("#scan_char").focus().val('CLOSECTN').trigger(eneterKey);
}
Nisarg Shah
  • 14,151
  • 6
  • 34
  • 55
  • @JSTai That's strange. I did verify that. Looks like it is not sending the value of `event.which` inside the event handler. So it might not be working because of that. – Nisarg Shah Dec 08 '17 at 09:53
0

This code below work fine for me.

  $('#clo_cart').click( function () {

     $("#scan_char").focus().val('CLOSECTN').trigger(enterKey());

  }//end of button click

    /* Function to allow program keypress ENTER */
    function enterKey() {

       return $.Event( "keypress", { which: 13 } );

    }
JSTai
  • 35
  • 1
  • 7
0

You can use this code,

$('#clo_cart').click( function () {

var e = $.Event( "keypress", { which: 13 } );
$("#scan_char").focus().val('CLOSECTN').trigger(e);

}//end of bu
Abhilash Ravindran C K
  • 1,818
  • 2
  • 13
  • 22