Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Font Size Based On Word Count Jquery

I have multiple articles, but they all need to fit in the same space. Characters are limited to 140. What I'm looking to do is resize the font size based on characters in my paragraph. So if the paragraph has less characters, I would like the font size to get bigger & vise versa.

My code below doesn't seem to be working and was wondering if someone would be able to help me?

What's happening at the moment, it seems to be taking the last else, because the font size from all paragraphs are 8em :(

Very much appreciate any help & thanks in advance!

Here is the code:

    $(function(){
    var $quote = $(".question p");
    var $numWords = $quote.text().split("").length;     
    if (($numWords >= 1) && ($numWords < 20)) {
        $quote.css("font-size", "2.2em");
    }
    else if (($numWords >= 20) && ($numWords < 60)) {
        $quote.css("font-size", "1.8em");
    }
    else if (($numWords >= 60) && ($numWords < 100)) {
        $quote.css("font-size", "1.2em");
    }
    else if (($numWords >= 100) && ($numWords < 140)) {
        $quote.css("font-size", "0.9em");
    }
    else {
        $quote.css("font-size", "0.8em");
    }           
});
like image 816
dennisterrey Avatar asked Feb 17 '23 12:02

dennisterrey


2 Answers

Your problem is you're not treating each paragraph individually: see http://jsfiddle.net/wkEMK/1/

$(function(){
    $(".question p").each(function () {
        var numChars = $(this).text().length;     
        if ((numChars >= 1) && (numChars < 20)) {
            $(this).css("font-size", "2.2em");
        }
        else if ((numChars >= 20) && (numChars < 60)) {
            $(this).css("font-size", "1.8em");
        }
        else if ((numChars >= 60) && (numChars < 100)) {
            $(this).css("font-size", "1.2em");
        }
        else if ((numChars >= 100) && (numChars < 140)) {
            $(this).css("font-size", "0.9em");
        }
        else {
            $(this).css("font-size", "0.8em");
        }           
    });
});

Your original code was getting the character count for ALL paragraphs that matched '.question p'. e.g. If you had two paragraphs, one with ten characters, the other with twenty characters, your JS would run once with a total of thirty, rather than processing each paragraph in turn.

like image 76
ChrisC Avatar answered Feb 20 '23 07:02

ChrisC


Do it like this using the .css function.. This way it will change each p elements font-size according to the number of chars inside each p element

$(function(){         
    $(".question p").css('font-size',function(){
        var $numWords = $(this).text().length; // get length of text for current p element
        if (($numWords >= 1) && ($numWords < 20)) {
            return "2.2em";
        }
        else if (($numWords >= 20) && ($numWords < 60)) {
            return "1.8em";
        }
        else if (($numWords >= 60) && ($numWords < 100)) {
            return "1.2em";
        }
        else if (($numWords >= 100) && ($numWords < 140)) {
            return "0.9em";
        }
        else {
            return "0.8em";
        }           
    });    
});

FIDDLE

like image 23
wirey00 Avatar answered Feb 20 '23 08:02

wirey00