It counts all the words within your target element. In the example above, we target a .post-content class selector.
The word count includes all text elements (headings, paragraphs, lists, blockquotes etc.). Everything relevant.
It excludes whitespace, commas etc.
Replace the .post-content class with whatever class, id, or element selector you have for your posts or pages.
Note: you can use getElementsByClassName() instead of querySelectorAll().
You test can with the code in your favorite browser’s console, or on this CodePen.
Has this been helpful? Let me know in the comments!
I’m using the code you just saw to build a Word Counter component for TechStacker’s content. Is anyone interested in a tutorial on how to do that?
I’m experimenting with ”Code Quickies” on TechStacker. I’m curious how many of you are actually just looking for working code snippets, as opposed to in-depth tutorials (which I also do)?