Search code examples
javascriptjquery-mobilekeyuponkeyup

keypress, keyup or keydown only work on the first ring in JQM


in my project have a jQuery Mobile textarea called "topic" on one page

I have this script I am using to count the characters entered in the textarea, but this script just gives me a result on the first keystroke. the other does not.

$(document).delegate("#topicDialog", "pageinit", function() {
$("#topic").keyup(function(e) {
        var tam = $(this).length;

        if (tam <= 61700){
            $("#maxCarac").html(tam +" typed characters. The maximum allowed is 61700");
        } else {
            alert("You have reached the maximum text size.\nPlease break your text into more than one topic.");
            $("#topic").val($("#topic").substring(61700));
        }
    });
});

example in action

What can be happening?


Solution

  • this line needs to be changed

    var tam = $(this).val().length;
    

    See Demo