Doing a Deep Word Count of a DIV
One option that occurred to me is to loop through all nodes in the div and count the words in the nodeValue if it's a text node, or otherwise loop through the element's sub-nodes if it has them. You could do this recursively and therefore find all text nodes within the DIV and its sub-elements.
The problem is this doesn't cover everything. Inputs and textareas have a value, not a nodeValue. You will want to ignore any "script" elements and perhaps others. Of course you can set up a list of exceptions and modify the algorithm a bit to allow for them.
An attempt at the approach described so far is shown here:
But what about select elements? Do you want the value or do you want the text inside the selected option? What if they're multi-select lists? etc.
Also is there a better overall approach than this recursive search with a bunch of exceptions?
Is this a thread? Or you have opened a Thread by mistake, while trying to give an answer to somebody?
It's meant to be a new thread, to ask for general ideas for how to best approach the problem. Sorry if it wasn't clear or if this is not the forum for such a broad and general question.
I saw someone talking about the challenges of making a word-count somewhere else on the web and thought I'd have a go at it but it has proven more complicated than I expected.
You must not worry about the text within the select options. From the DOM point of view they are not different form the texts within, say, a SPAN. But keep in mind that, in fact, in case of the SELECT elements, the text has no relevance. The value is important, not the text, because the value is submitted.
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)