dcsimg
www.webdeveloper.com
Results 1 to 4 of 4

Thread: Doing a Deep Word Count of a DIV

  1. #1
    Join Date
    Nov 2010
    Posts
    86

    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:
    http://webinterfacetricks.com/word_count/

    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?

  2. #2
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Is this a thread? Or you have opened a Thread by mistake, while trying to give an answer to somebody?

  3. #3
    Join Date
    Nov 2010
    Posts
    86
    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.

  4. #4
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    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.

    Thus, you problem regarding the values of the form's elements is meaningless. When you say that you want to search for the textNodes, then go searching for the textNodes. But the value is an attribute (or straight a JavaScript property) of an element, not a textNode. So, I don't see why do you mix different categories (textNodes with attributesNodes(or JS properties) within the same pot.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles