How to count words in JavaScript using JQuery

前端 未结 7 1198
旧巷少年郎
旧巷少年郎 2020-12-09 13:17

I have a simple html text box. When I \"submit\" the form that the text box is in, I would like to get a variable with the number of words inside using Jqu

7条回答
  •  無奈伤痛
    2020-12-09 13:59

    Is very useful to remove whitespaces from the beginning and end of the string usign $.trim(). You can use keyup event for a realtime counting.

    $('#name').keyup(function(){
        var words = $.trim($(this).val()).split(' ');
        console.log(words.length);
    });
    

提交回复
热议问题