简体   繁体   中英

Chracter deletion on reaching character limit or enable disable div on backspace

We have a disabled div on reaching to maximum character limit, which is need to be enable on back space. But now we are not getting the ASCII code for back space in iPhone. How could i detect back space tap for div. Here is my code for character limit in j Query and need to enable so that i can delete character:

<script>
var charlimit = 20;
var maindiv = 'content';
var charlendiv = 'myDiv';
$().ready(function()
   {
    var div = document.getElementById (maindiv);
    var text = $(div).text();
    document.getElementById(charlendiv).innerHTML = charlimit - text.length;
        $('#'+maindiv).keypress(function(e)
             { 
        var div = document.getElementById (maindiv);
            var text = $(div).text();
        var code = e.keyCode || e.which;
        if (text.length < charlimit) 
             {
          div.contentEditable = true;
         }
        else
              {
           div.contentEditable = false;
           if(code=='08' || code=='46' || code=='')  //ASCII code not detect by keyboard back space
                {
             div.contentEditable = true;
             }
         }
    });
    $('#'+maindiv).keyup(function(e)
       {
        var code = e.keyCode || e.which;
        var div = document.getElementById (maindiv);
        var text = $(div).text();
        document.getElementById(charlendiv).innerHTML = charlimit - text.length;
    });
});
</script>

The div has been disabled so none of your key will work now. Better approach is not to disable the div, instead you can opt for changing color of the div when limit has reached.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM