Skip to content Skip to sidebar Skip to footer

Count Number Of Characters In Each Paragraph

i am trying to find a way to count the number of characters in every paragraph on a page. I discovered this little snippet below which counts the number of words in each paragraph

Solution 1:

here is the solution:

var count = $('p').text().length;

So your code will be like:

$(document).ready(function() {
    $('p').each(function(i) {
        var iTotalWords = $(this).text().split(' ').length;
        var charCount = $(this).text().length;
        $(this).append("<b> " + iTotalWords + " words and " + charCount + " chars </b>");
    });
})

Hope this helps you ;D


Solution 2:

$(document).ready(function() {
    $('p').each(function(i) {
        var text = $(this).text();
        var iTotalWords = text.split(' ').length;
        var iTotalChars = text.length;
        $(this).append("<b> " + iTotalWords + " words and " + iTotalChars + " chars</b>");
    });
})

Solution 3:

$(document).ready(function() {
    $('p').each(function(i) {
        var iTotalChars = $(this).text().length;
        $(this).append("<b> " + iTotalChars + " characters</b>");
    });
})
<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
    <p>123456789</p>
</html>

Post a Comment for "Count Number Of Characters In Each Paragraph"