Count textarea characters

前端 未结 11 2085
走了就别回头了
走了就别回头了 2020-11-28 07:29

I am developing a character count for my textarea on this website. Right now, it says NaN because it seems to not find the length of how many characters are in the field, wh

11条回答
  •  没有蜡笔的小新
    2020-11-28 07:52

    $("#textarea").keyup(function(){
      $("#count").text($(this).val().length);
    });
    

    The above will do what you want. If you want to do a count down then change it to this:

    $("#textarea").keyup(function(){
      $("#count").text("Characters left: " + (500 - $(this).val().length));
    });
    

    Alternatively, you can accomplish the same thing without jQuery using the following code. (Thanks @Niet)

    document.getElementById('textarea').onkeyup = function () {
      document.getElementById('count').innerHTML = "Characters left: " + (500 - this.value.length);
    };
    

提交回复
热议问题