2

I have a Javascript function that works fine on PC, but it doesn't work when loaded on mobile. I think it is because mobile treats tap and click differently, but how do I modify the following function so that it works on mobile?

(function () {
    var a = { 48: "0", 49: "1", 50: "2", 51: "3", 52: "4", 53: "5", 54: "6", 55: "7", 56: "8", 57: "9", 96: "0", 97: "1", 98: "2", 99: "3", 100: "4", 101: "5", 102: "6", 103: "7", 104: "8", 105: "9", 106: "x", 107: "+", 109: "-", 110: ".", 111: "\u00f7", 8: "backspace", 13: "=", 46: "c", 67: "c" }, b = function (b) {
        if (b.keyCode in a) {
            var c = document.getElementById("button-" + a[b.keyCode]);
            c.className.match(/(\s|^)active(\s|$)/) || (c.className += " active");
            setTimeout(function () {
                c.className.match(/(\s|^)active(\s|$)/) && (c.className = c.className.replace(/(\s|^)active(\s|$)/, " "))
            }, 100);
            document.createEvent ? (b = document.createEvent("HTMLEvents"), b.initEvent("click", !0, !0), c.dispatchEvent(b)) : (b = document.createEventObject(), c.fireEvent("onclick", b))
        }
    };
    document.addEventListener ? document.addEventListener("keyup", b, !1) : document.attachEvent && document.attachEvent("keyup", b)
})();
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Julia
  • 1,207
  • 4
  • 29
  • 47
  • attachEvent? Supporting IE6? – epascarello Aug 23 '17 at 17:17
  • I'm not sure actually as I didn't write this function and I'm not really good with javascript. I tried on iPhone and Android browser and the function is not working. – Julia Aug 23 '17 at 17:19
  • The original author wasn't "good" either, the IE < 9 hack should be `document.attachEvent("onkeyup", b)`. Also, the event creation is a bit old, see https://developer.mozilla.org/en-US/docs/Web/API/Document/createEvent – Teemu Aug 23 '17 at 17:22
  • 1
    Possible duplicate of [onClick not working on mobile (touch)](https://stackoverflow.com/questions/22015908/onclick-not-working-on-mobile-touch) – Brian Tompsett - 汤莱恩 Jul 14 '19 at 16:22

0 Answers0