keypress and keyup jQuery events not firing on Android

  • 1
  • Problem
  • Updated 4 years ago
  • Not a Problem
I have a app that uses a textbox to search a table for a matching input. It uses the Keypress or keyup event to start the sorting. It works fine in a browser, but will not work on an android build. I'm sure that the function is getting loaded in, but the event is not firing. Here is my code:

// Write on keyup event of keyword input element
$( document ).ready(function() {
alert("Loaded Search!");

$("#search").keypress(function(){
console.log("Press!");
_this = this;
alert("Press")
$.each($("#table tbody").find("tr"), function() {
console.log($(this).text());
alert($(this).text());
if($(this).text().toLowerCase().indexOf($(_this).val().toLowerCase()) == -1)
$(this).hide();
else
$(this).show();
});
});
});
Photo of axtscz

axtscz

  • 2 Posts
  • 0 Reply Likes
  • confused

Posted 5 years ago

  • 1
Photo of Petra V.

Petra V., Champion

  • 7794 Posts
  • 1391 Reply Likes
Don't use the keypress event, as it is deprecated.
See http://stackoverflow.com/questions/21...

Also see https://code.google.com/p/android/iss...
Photo of axtscz

axtscz

  • 2 Posts
  • 0 Reply Likes
Well the issue is that keyup is not being called either.
Photo of Petra V.

Petra V., Champion

  • 7794 Posts
  • 1391 Reply Likes
This may be related:
https://code.google.com/p/chromium/is...

Wouldn't it be wise to first isolate the problem? You don't want to just shoot in the dark (or sit and wait)....
- Use the Chrome browser and look for javascript errors
- Use several Android flavors/versions to see if it behaves differently for 4.0, 4.1, 4.4 and 5
- For test purposes, write a vanilla javascript eventhandler and add an event listener to see if it works without jQuery.
- With jQuery, see if the click or vclick events can be used instead.
- With jQuery, force a keyup event (calling the keyup() method without parameters for the #search field) and see if you can catch that.
Photo of Mayank Mittal

Mayank Mittal

  • 1 Post
  • 0 Reply Likes
I will suggest two events on input box.

**This will work on desktop webbrowser.**

**1) onkeypress = return isNumberKey(event);**

function isNumberKey(e)
{

var evt = e || window.event;

if(evt)
{
var charCode = evt.keyCode || evt.which;
}
else
{
return true;
}

if((charCode > 47 && charCode < 58) || charCode == 9 || charCode == 8 || charCode ==46 || charCode ==37 || charCode==39)
{
return true;
}

return false;
}

**This will work for mobile**

**2) onkeyup="numberMobile(event);"**

function numberMobile(e){
e.target.value = e.target.value.replace(/[^\d]/g,'');
return false;
}

Apply both event on the input box and it will work.Only drawback is,It make it slow.But for now we have to live with it.