2

This is my code to countdown characters. It work fine in Firefox, Chrome but it not work in IE . Can you help me solve it and explain the reason.

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>CountDown Character</title>
    <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
</head>
<body>
    <form action="">
        <textarea name="message1" id="message1" cols="30" rows="10" data-limit="10"></textarea>
        <br>
        <span name="remLen1" id="remLen1">10</span>
    </form>
    <SCRIPT LANGUAGE="JavaScript">
        $(document).ready(function(){
            $('#message1').on("keyup keydown",
            function () {
                var limit = $(this).data("limit");
                var remainingChars = limit - $(this).val().length;
                console.log(remainingChars);
                if (remainingChars <= 0) {
                    $('#message1').val($('#message1').val().substring(0,limit));
                }
                $("#remLen1").html(remainingChars<=0?0:remainingChars);
            });
        });
    </script>
</body>
</html>

Thanks. Ex: I'm using countdown japanese characters and alpha character.

JoriO
  • 1,050
  • 6
  • 13
tuandng8
  • 23
  • 4
  • ` – Raptor Nov 04 '14 at 06:40
  • Thanks. I will repair it. – tuandng8 Nov 04 '14 at 06:42
  • I have been removed it and it was worked fine. But now i have a problem. When i press the 11th character (japanese character) it replace old string by new character and coutdown 9. – tuandng8 Nov 04 '14 at 06:51
  • Japanese characters (and many other non-ASCII) uses multi-byte to represent a character. See [this](http://stackoverflow.com/questions/5515869/string-length-in-bytes-in-javascript) – Raptor Nov 04 '14 at 06:54
  • I use alert() show the string after substring, It return value true, but it display in textarea not true. It display new character. Why this? – tuandng8 Nov 04 '14 at 07:24

1 Answers1

0

I tried your code and found that console.log(remainingChars); is causing problem as it gives error : console is undefined. Remove this line and your code will work.

$(document).ready(function(){
    $('#message1').on("keyup keydown",
    function () {
        var limit = $(this).data("limit");
        var remainingChars = limit - $(this).val().length;
        /*console.log(remainingChars);*/
        if (remainingChars <= 0) {
            $('#message1').val($('#message1').val().substring(0,limit));
        }
        $("#remLen1").html(remainingChars<=0?0:remainingChars);
    });
});

And if you still want to use console, then see this for solution.

Community
  • 1
  • 1
Bhushan Kawadkar
  • 28,279
  • 5
  • 35
  • 57