Home get index of a searched word in a page jquery
Reply: 0

get index of a searched word in a page jquery

user2810
1#
user2810 Published in April 25, 2018, 8:22 pm

I have a div containg following text. I want to search like an editor. I have tried many time to solve my problem but I didn't found any solution.Please guide me how can I resolve my problem.

  <div id="content">
        <p>
            Lorem Ipsum is simply dummy text of the printing and typesetting industry.
            Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,
            when an unknown printer took a galley of type and scrambled it to make a type specimen book.
            It has survived not only five centuries, but also the leap into electronic typesetting,
            remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset
            sheets containing Lorem Ipsum passages, and more recently with desktop publishing software
            like Aldus PageMaker including versions of Lorem Ipsum.
        </p>
        <p>
            Lorem Ipsum is simply dummy text of the printing and typesetting industry.
            Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,
            when an unknown printer took a galley of type and scrambled it to make a type specimen book.
            It has survived not only five centuries, but also the leap into electronic typesetting,
            remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset
            sheets containing Lorem Ipsum passages, and more recently with desktop publishing software
            like Aldus PageMaker including versions of Lorem Ipsum.
        </p>
    </div>

Now I have selected a word for example first dummy in first paragraph, it should return 1 out of 4.Right now it alerts only 4(total count of dummy in div)

Thanx in anticipation.My jquery is

 $(document).ready(function () {
            $('#content').mouseup(function () {
                var sel = window.getSelection().toString();
                wordCount = occurrences(sel);
                alert(wordCount);
            })
        })
        function occurrences(subString, allowOverlapping) {
            string = $('#content').text();
            subString += "";
            if (subString.length <= 0)
                return (string.length + 1);
            var n = 0,
                    pos = 0,
                    step = allowOverlapping ? 1 : subString.length;
            while (true) {
                pos = string.indexOf(subString, pos);
                if (pos >= 0) {
                    ++n;
                    pos += step;
                } else
                    break;
            }
            return n;
        }
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.427238 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO