jQuery: keyup event for mobile device












8















I'm having a few issues getting a keyup event to fire on my iPhone, my code is as follows:



        var passwordArray = ["word", "test", "hello", "another", "here"];

var test = document.getElementById('enter-password');
test.addEventListener('keyup', function(e) {

if (jQuery.inArray(this.value, passwordArray) != -1) {
alert("THIS IS WORKING");
} else {}

});


The idea being that as the user is typing into the #enter-password field, as and when they've matched a word in the passwordArray the alert will fire. This works on desktop, e.g. once you've entered word the function will fire straight away as soon as you've typed the d. Is there anyway to get this to work for mobile too?










share|improve this question



























    8















    I'm having a few issues getting a keyup event to fire on my iPhone, my code is as follows:



            var passwordArray = ["word", "test", "hello", "another", "here"];

    var test = document.getElementById('enter-password');
    test.addEventListener('keyup', function(e) {

    if (jQuery.inArray(this.value, passwordArray) != -1) {
    alert("THIS IS WORKING");
    } else {}

    });


    The idea being that as the user is typing into the #enter-password field, as and when they've matched a word in the passwordArray the alert will fire. This works on desktop, e.g. once you've entered word the function will fire straight away as soon as you've typed the d. Is there anyway to get this to work for mobile too?










    share|improve this question

























      8












      8








      8








      I'm having a few issues getting a keyup event to fire on my iPhone, my code is as follows:



              var passwordArray = ["word", "test", "hello", "another", "here"];

      var test = document.getElementById('enter-password');
      test.addEventListener('keyup', function(e) {

      if (jQuery.inArray(this.value, passwordArray) != -1) {
      alert("THIS IS WORKING");
      } else {}

      });


      The idea being that as the user is typing into the #enter-password field, as and when they've matched a word in the passwordArray the alert will fire. This works on desktop, e.g. once you've entered word the function will fire straight away as soon as you've typed the d. Is there anyway to get this to work for mobile too?










      share|improve this question














      I'm having a few issues getting a keyup event to fire on my iPhone, my code is as follows:



              var passwordArray = ["word", "test", "hello", "another", "here"];

      var test = document.getElementById('enter-password');
      test.addEventListener('keyup', function(e) {

      if (jQuery.inArray(this.value, passwordArray) != -1) {
      alert("THIS IS WORKING");
      } else {}

      });


      The idea being that as the user is typing into the #enter-password field, as and when they've matched a word in the passwordArray the alert will fire. This works on desktop, e.g. once you've entered word the function will fire straight away as soon as you've typed the d. Is there anyway to get this to work for mobile too?







      javascript jquery mobile click keyup






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Aug 17 '16 at 6:31









      user1374796user1374796

      80193569




      80193569
























          4 Answers
          4






          active

          oldest

          votes


















          19














          You can add input event. It is an event that triggers whenever the input changes. Input works both on desktop as well as mobile phones



          test.on('keyup input', function(){
          //code
          });


          You can check this answer for more details on jQuery Input Event






          share|improve this answer

































            6














            There are three events you can use (but you have to be careful on how you "combine" them):




            • keyup : it works on devices with a keyboard, it's triggered when you release a key (any key, even keys that don't show anything on the screen, like ALT or CTRL);


            • touchend: it works on touchscreen devices, it's triggered when you remove your finger/pen from the display;


            • input: it's triggered when you press a key "and the input changes" (if you press keys like ALT or CTRL this event is not fired).



            The input event works on keyboard devices and with touchscreen devices, it's important to point this out because in the accepted answer the example is correct but approximative:



            test.on('keyup input', function(){
            }


            On keyboard based devices, this function is called twice because both the events keyup and input will be fired.



            The correct answer should be:



            test.on('keyup touchend', function(){
            }


            (the function is called on keyup for desktops/laptops OR on touchend for mobiles)



            or you can just simply use



            test.on('input', function(){
            }


            but remember that the input event will not be triggered by all keys (CTRL, ALT & co. will not fire the event).






            share|improve this answer

































              1














              The touchend event is fired when a touch point is removed from the device.



              https://developer.mozilla.org/en-US/docs/Web/Events/touchend



              You can pass keyup and touchend events into the .on() jQuery method (instead of the keyup() method) to trigger your code on both of these events.



              test.on('keyup touchend', function(){
              //code
              });





              share|improve this answer































                0














                You should add a input event. It works on both mobile and computer devices.






                share|improve this answer
























                • It would be good to add more information to your response. See the FAQ and try to update your answer

                  – GMB
                  Dec 9 '18 at 18:04











                Your Answer






                StackExchange.ifUsing("editor", function () {
                StackExchange.using("externalEditor", function () {
                StackExchange.using("snippets", function () {
                StackExchange.snippets.init();
                });
                });
                }, "code-snippets");

                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "1"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                draft saved

                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f38989559%2fjquery-keyup-event-for-mobile-device%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                4 Answers
                4






                active

                oldest

                votes








                4 Answers
                4






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                19














                You can add input event. It is an event that triggers whenever the input changes. Input works both on desktop as well as mobile phones



                test.on('keyup input', function(){
                //code
                });


                You can check this answer for more details on jQuery Input Event






                share|improve this answer






























                  19














                  You can add input event. It is an event that triggers whenever the input changes. Input works both on desktop as well as mobile phones



                  test.on('keyup input', function(){
                  //code
                  });


                  You can check this answer for more details on jQuery Input Event






                  share|improve this answer




























                    19












                    19








                    19







                    You can add input event. It is an event that triggers whenever the input changes. Input works both on desktop as well as mobile phones



                    test.on('keyup input', function(){
                    //code
                    });


                    You can check this answer for more details on jQuery Input Event






                    share|improve this answer















                    You can add input event. It is an event that triggers whenever the input changes. Input works both on desktop as well as mobile phones



                    test.on('keyup input', function(){
                    //code
                    });


                    You can check this answer for more details on jQuery Input Event







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited May 23 '17 at 11:46









                    Community

                    11




                    11










                    answered Aug 19 '16 at 5:49









                    JoysonJoyson

                    2,26811025




                    2,26811025

























                        6














                        There are three events you can use (but you have to be careful on how you "combine" them):




                        • keyup : it works on devices with a keyboard, it's triggered when you release a key (any key, even keys that don't show anything on the screen, like ALT or CTRL);


                        • touchend: it works on touchscreen devices, it's triggered when you remove your finger/pen from the display;


                        • input: it's triggered when you press a key "and the input changes" (if you press keys like ALT or CTRL this event is not fired).



                        The input event works on keyboard devices and with touchscreen devices, it's important to point this out because in the accepted answer the example is correct but approximative:



                        test.on('keyup input', function(){
                        }


                        On keyboard based devices, this function is called twice because both the events keyup and input will be fired.



                        The correct answer should be:



                        test.on('keyup touchend', function(){
                        }


                        (the function is called on keyup for desktops/laptops OR on touchend for mobiles)



                        or you can just simply use



                        test.on('input', function(){
                        }


                        but remember that the input event will not be triggered by all keys (CTRL, ALT & co. will not fire the event).






                        share|improve this answer






























                          6














                          There are three events you can use (but you have to be careful on how you "combine" them):




                          • keyup : it works on devices with a keyboard, it's triggered when you release a key (any key, even keys that don't show anything on the screen, like ALT or CTRL);


                          • touchend: it works on touchscreen devices, it's triggered when you remove your finger/pen from the display;


                          • input: it's triggered when you press a key "and the input changes" (if you press keys like ALT or CTRL this event is not fired).



                          The input event works on keyboard devices and with touchscreen devices, it's important to point this out because in the accepted answer the example is correct but approximative:



                          test.on('keyup input', function(){
                          }


                          On keyboard based devices, this function is called twice because both the events keyup and input will be fired.



                          The correct answer should be:



                          test.on('keyup touchend', function(){
                          }


                          (the function is called on keyup for desktops/laptops OR on touchend for mobiles)



                          or you can just simply use



                          test.on('input', function(){
                          }


                          but remember that the input event will not be triggered by all keys (CTRL, ALT & co. will not fire the event).






                          share|improve this answer




























                            6












                            6








                            6







                            There are three events you can use (but you have to be careful on how you "combine" them):




                            • keyup : it works on devices with a keyboard, it's triggered when you release a key (any key, even keys that don't show anything on the screen, like ALT or CTRL);


                            • touchend: it works on touchscreen devices, it's triggered when you remove your finger/pen from the display;


                            • input: it's triggered when you press a key "and the input changes" (if you press keys like ALT or CTRL this event is not fired).



                            The input event works on keyboard devices and with touchscreen devices, it's important to point this out because in the accepted answer the example is correct but approximative:



                            test.on('keyup input', function(){
                            }


                            On keyboard based devices, this function is called twice because both the events keyup and input will be fired.



                            The correct answer should be:



                            test.on('keyup touchend', function(){
                            }


                            (the function is called on keyup for desktops/laptops OR on touchend for mobiles)



                            or you can just simply use



                            test.on('input', function(){
                            }


                            but remember that the input event will not be triggered by all keys (CTRL, ALT & co. will not fire the event).






                            share|improve this answer















                            There are three events you can use (but you have to be careful on how you "combine" them):




                            • keyup : it works on devices with a keyboard, it's triggered when you release a key (any key, even keys that don't show anything on the screen, like ALT or CTRL);


                            • touchend: it works on touchscreen devices, it's triggered when you remove your finger/pen from the display;


                            • input: it's triggered when you press a key "and the input changes" (if you press keys like ALT or CTRL this event is not fired).



                            The input event works on keyboard devices and with touchscreen devices, it's important to point this out because in the accepted answer the example is correct but approximative:



                            test.on('keyup input', function(){
                            }


                            On keyboard based devices, this function is called twice because both the events keyup and input will be fired.



                            The correct answer should be:



                            test.on('keyup touchend', function(){
                            }


                            (the function is called on keyup for desktops/laptops OR on touchend for mobiles)



                            or you can just simply use



                            test.on('input', function(){
                            }


                            but remember that the input event will not be triggered by all keys (CTRL, ALT & co. will not fire the event).







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Aug 31 '18 at 23:25









                            Benjamin

                            15.9k29122231




                            15.9k29122231










                            answered Feb 28 '18 at 12:33









                            FrankFrank

                            3981510




                            3981510























                                1














                                The touchend event is fired when a touch point is removed from the device.



                                https://developer.mozilla.org/en-US/docs/Web/Events/touchend



                                You can pass keyup and touchend events into the .on() jQuery method (instead of the keyup() method) to trigger your code on both of these events.



                                test.on('keyup touchend', function(){
                                //code
                                });





                                share|improve this answer




























                                  1














                                  The touchend event is fired when a touch point is removed from the device.



                                  https://developer.mozilla.org/en-US/docs/Web/Events/touchend



                                  You can pass keyup and touchend events into the .on() jQuery method (instead of the keyup() method) to trigger your code on both of these events.



                                  test.on('keyup touchend', function(){
                                  //code
                                  });





                                  share|improve this answer


























                                    1












                                    1








                                    1







                                    The touchend event is fired when a touch point is removed from the device.



                                    https://developer.mozilla.org/en-US/docs/Web/Events/touchend



                                    You can pass keyup and touchend events into the .on() jQuery method (instead of the keyup() method) to trigger your code on both of these events.



                                    test.on('keyup touchend', function(){
                                    //code
                                    });





                                    share|improve this answer













                                    The touchend event is fired when a touch point is removed from the device.



                                    https://developer.mozilla.org/en-US/docs/Web/Events/touchend



                                    You can pass keyup and touchend events into the .on() jQuery method (instead of the keyup() method) to trigger your code on both of these events.



                                    test.on('keyup touchend', function(){
                                    //code
                                    });






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Aug 17 '16 at 6:35







                                    user5116395






























                                        0














                                        You should add a input event. It works on both mobile and computer devices.






                                        share|improve this answer
























                                        • It would be good to add more information to your response. See the FAQ and try to update your answer

                                          – GMB
                                          Dec 9 '18 at 18:04
















                                        0














                                        You should add a input event. It works on both mobile and computer devices.






                                        share|improve this answer
























                                        • It would be good to add more information to your response. See the FAQ and try to update your answer

                                          – GMB
                                          Dec 9 '18 at 18:04














                                        0












                                        0








                                        0







                                        You should add a input event. It works on both mobile and computer devices.






                                        share|improve this answer













                                        You should add a input event. It works on both mobile and computer devices.







                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Dec 9 '18 at 17:33







                                        user10643043




















                                        • It would be good to add more information to your response. See the FAQ and try to update your answer

                                          – GMB
                                          Dec 9 '18 at 18:04



















                                        • It would be good to add more information to your response. See the FAQ and try to update your answer

                                          – GMB
                                          Dec 9 '18 at 18:04

















                                        It would be good to add more information to your response. See the FAQ and try to update your answer

                                        – GMB
                                        Dec 9 '18 at 18:04





                                        It would be good to add more information to your response. See the FAQ and try to update your answer

                                        – GMB
                                        Dec 9 '18 at 18:04


















                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Stack Overflow!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f38989559%2fjquery-keyup-event-for-mobile-device%23new-answer', 'question_page');
                                        }
                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        404 Error Contact Form 7 ajax form submitting

                                        How to know if a Active Directory user can login interactively

                                        TypeError: fit_transform() missing 1 required positional argument: 'X'