show-notice
hide-notice

Thursday 15 August 2013

count words in each paragraph using jquery


Introduction:

Find jQuery code to count number of words in each paragraph or HTML <p> tag element. Below jQuery code does following things.

HTML:
Para 1:
gjhdfj jghdjghdgj jg jkghdjfkg jgdjghdkj hgghjdgh jghdj gjdhgjdhg   jgdhgdjk jgdh jdg
Para 2: hgdjfghdgh jghdfgj hgjdhgj hgjd hgjfdhgj hj ghjdfgh djghj hgfj hjgh jgh jghdj hgdj hdgjhd gjh jdgh jdghjd
Para 3: gdfgjk jghdj hgjd hjg hdjg dhjgd
JS:
$(document).ready(function() {
    $('p').each(function(i) {
        var iTotalWords = $(this).text().split(' ').length;
        $(this).append(" " + iTotalWords + " words ");
    });
});

SHARE THIS POST   

0 comments :

Post a Comment

Design by Gohilinfotech | www.gohilinfotech.blogspot.com