Change jquery plugin from word count to character plugin?

I am trying to change this plugin so that it takes into account characters, not words.

$(function(){

    var $quote = $(".post p:first");

    var $numWords = $quote.text().split(" ").length;

    if (($numWords >= 1) && ($numWords < 10)) {
        $quote.css("font-size", "36px");
    }
    else if (($numWords >= 10) && ($numWords < 20)) {
        $quote.css("font-size", "32px");
    }
    else if (($numWords >= 20) && ($numWords < 30)) {
        $quote.css("font-size", "28px");
    }
    else if (($numWords >= 30) && ($numWords < 40)) {
        $quote.css("font-size", "24px");
    }
    else {
        $quote.css("font-size", "20px");
    }    

});

Here's the original post in the plugin: http://css-tricks.com/set-font-size-based-on-word-count/

Thank!

+3
source share
1 answer

It: $quote.text().split(" ").length

It must be: $quote.text().length.

split(" ")Divides the text with spaces, giving you words. By deleting this part, you count characters instead of words.

+3
source

Source: https://habr.com/ru/post/1780789/


All Articles