Advanced Search

Results 1 to 2 of 2

Thread: Count Words as they are being selected/highlighted

  1. #1
    Join Date
    Aug 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Count Words as they are being selected/highlighted

    Hi,

    I have been searching the net for a script to accomplish the following:

    Within a div container there are about 30 words per 'container' like so -->

    #word1 #word2 #word3 #word4 ...and so on... (word includes the hash #)...

    I am trying to find a method for when a person selects/highlights each word it counts them and displays how many words are being selected/highlighted.

    Does anyone know where there is an example of this? or can anyone provide such

    I have searched for days and have come up empty, therefore this post!

    Many thanks in advance for your help

  2. #2
    Join Date
    Jan 2008
    Posts
    4,158
    Thanks
    28
    Thanked 623 Times in 619 Posts
    Blog Entries
    1

    Default

    The following line of code will equal the number of array values present when the selected text is split by a space.
    Code:
    window.getSelection().toString().split(/\s/).length;
    window.getSelection() returns a selection object which can be converted into a string containing the exact characters selected. Afterwards, you need to apply a simple split by space function (using split()) and get the length of the array returned.

    https://developer.mozilla.org/en-US/...w.getSelection
    https://developer.mozilla.org/en-US/...s/String/split
    Last edited by Nile; 08-17-2012 at 07:31 PM.

Bookmarks

Posting Permissions

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