5

I am trying to use the code below posted by Biff MaGriff for an earlier solution to preventing backspace use that causes the browser to go back a page in history. My issue is that the code will not allow backspace (deletion) of characters in type="number".

$(document).keydown(function (e) {
    var preventKeyPress;
    if (e.keyCode == 8) {
        var d = e.srcElement || e.target;
        switch (d.tagName.toUpperCase()) {
            case 'TEXTAREA':
                preventKeyPress = d.readOnly || d.disabled;
                break;
            case 'INPUT':
                preventKeyPress = d.readOnly || d.disabled ||
                    (d.attributes["type"] && $.inArray(d.attributes["type"].value.toLowerCase(), ["radio", "checkbox", "submit", "button"]) >= 0);
                break;
            case 'DIV':
                preventKeyPress = d.readOnly || d.disabled || !(d.attributes["contentEditable"] && d.attributes["contentEditable"].value == "true");
                break;
            default:
                preventKeyPress = true;
                break;
        }
    }
    else
        preventKeyPress = false;

    if (preventKeyPress)
        e.preventDefault();
});

My code is as follows:

<input id="tesnprice" type="number" step=0.01 name="price" size="50" maxlength="6" />
Community
  • 1
  • 1
nwolybug
  • 462
  • 5
  • 12
  • 1
    It does allow backspace in inputs of type number. http://jsfiddle.net/adamconway/htm09sgo/ What is the problem? – Adam Aug 07 '14 at 19:17
  • Thanks for the response Adam. Further testing, you are correct that it works in another browser. I hadn't checked that. On Ubuntu 14.04 with Chrome 64bit browser, it does not. In Firefox it works fine. Will try to adjust the title to convey this update. – nwolybug Aug 08 '14 at 18:25
  • 1
    Hmm. Chrome (36) on Win 8 works fine. have you tried my fiddle in your chrome or some other code of your own? – Adam Aug 08 '14 at 18:35
  • Have seen this on another system in Firefox. Seems to work intermittently depending upon OS and browser. I've decided just to change the type="text" and all works well. Tested it in jsfiddle and it still has issues... Not going to bother with it unless someone else wants to experiment. – nwolybug Aug 22 '14 at 04:48

0 Answers0