How to count words in JavaScript using JQuery

前端 未结 7 1197
旧巷少年郎
旧巷少年郎 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);
    });
    
    0 讨论(0)
提交回复
热议问题