Hello There, Guest! or RegisterHelp Calendar Member List Search
Counting words using javascript


Share This Thread:
in
Solved
[How To?] Counting words using javascript
Good day,  Please I need someone to help me with a javascript that I can use to count the total of words inside a div element using it's 'id' I'll be very grateful if I get a help from this forum. Thanks in advance.
Here is a very simple javascript that can count words in a div element using it's 'id'. [justify]Let's assume below is your div element with the sentences.[/justify] <div id='yourDivId'> This is an example div with some text. Replace this with your own content.</div> [html] var divContent = document.getElementById('yourDivId').innerText || document.getElementById('yourDivId').textContent; var wordCount = divContent.split(/\s+/).filter(function(word) { return word.length > 0; }).length; document.write("Number of words: " + wordCount); [/html] [justify]Above is the javascript that will count the number of words inside the div element, you can change the ID to a unique one.[/justify] [justify]Thanks and welcome to the forum. Happy Coding! :smiling:[/justify]


Forum Jump:

Users browsing this thread: