Anh Hậu
Anh Hậu

Reputation: 91

[PhoneGap][Android] How to detect enter key

I have issue with phonegap on android, i have a simple html

 <form>
  <fieldset>
    <label for="target">Type Something:</label>
    <input id="target" type="text">
  </fieldset>
</form>

and a simple javascript

$("#target").on("keypress", function(event){               
    alert(event.which);
 });

When i press enter key, nothing happen, other keys work. I use phonegap 2.9.0 and android 2.3.5

Please help me

Upvotes: 1

Views: 8144

Answers (2)

Ryan Schlueter
Ryan Schlueter

Reputation: 2221

This is how I did it in my mobile app but was not with phonegap so not positive it will work, But worth a try.

   $('#target').submit(function (event) {
   event.preventDefault();
 alert(event.which);
}

Upvotes: 1

Andrew Lively
Andrew Lively

Reputation: 2153

You can use the keycodes to detect which key was pressed. I have an example here:

jsFiddle

HTML

<form>
    <fieldset>
        <label for="target">Type Something:</label>
        <input id="target" type="text" />
    </fieldset>
</form>

JavaScript

$("#target").on("keypress", function(event){               
    if (event.keyCode === 13) {
        alert("The enter key was pressed");
        event.preventDefault();
    }
});

Upvotes: 5

Related Questions