Note that there are some explanatory texts on larger screens.

plurals
  1. POFind the previous and next 10 words of a given string inside a p-tag with jQuery
    primarykey
    data
    text
    <p>I'm developing a project with html5. I'm searching a word in a div. I need to Highlight that word from the content and need to slice the content before and after that searched word.Here is my code.</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt; &lt;meta name="apple-mobile-web-app-capable" content="yes"&gt; &lt;meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" /&gt; &lt;script src="http://code.jquery.com/jquery-1.10.1.min.js"&gt;&lt;/script&gt; &lt;script&gt; $(document).ready(function(){ $('#search').click(function() { var keyword = $('.qid').val().replace(/\s+/g, " ").replace(/\s+$|^\s+/g, ""); var containsString = keyword; if(keyword == ""){ }else{ $('#search_content').css('overflow-y','scroll'); } $("p").removeClass('show'); containsString = "p:contains("+containsString+")".replace("p:contains()",""); $(containsString).addClass('show'); $('#search_content').show(); }); $('.srch').click(function(){ $('#displaybox2').show(); }); }); &lt;/script&gt; &lt;style&gt; .srch { background: none repeat scroll 0 0 #FFFFFF; color: #FFFFFF; cursor: pointer; float: right; height: 30px; margin-right: 27%; margin-top: 5px; padding: 2px 0 2px 3px; width: 30px; } #displaybox2 { border: 1px solid #FFFFFF; float: left; height: 552px; left: 1px; position: relative; width: 370px; z-index: 100000; } #search_content { float: left; width: 100%; position: relative; height: 510px; display: none; } p,h2{ display:none; } .qid{ margin-top:10px; } .show{ display: block; background-color:#FFFFFF; cursor:pointer; } .highlight{ background-color:#636F7C; } .srch_icon { width: 100%; } &lt;/style&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="displaybox2"&gt; &lt;input class="qid" type="text"/&gt;&lt;button id = "search"&gt;Search&lt;/button&gt; &lt;div id = "search_content"&gt; &lt;p&gt;Next evening was a lovely evening, and I walked out early to enjoy it. The sun was not yet quite down when I traversed the field-path near the top of the deep cutting. I would extend my walk for an hour, I said to myself, half an hour on and half an hour back, and it would then be time to go to my signal-man’s box.&lt;/p&gt; &lt;p&gt;Before pursuing my stroll, I stepped to the brink, and mechanically looked down, from the point from which I had first seen him. I cannot describe the thrill that seized upon me, when, close at the mouth of the tunnel, I saw the appearance of a man, with his left sleeve across his eyes, passionately waving his right arm.&lt;/p&gt; &lt;/div&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>If i search word 'extend' it returns the answer as </p> <blockquote> <p>Next evening was a lovely evening, and I walked out early to enjoy it. The sun was not yet quite down when I traversed the field-path near the top of the deep cutting. I would extend my walk for an hour, I said to myself, half an hour on and half an hour back, and it would then be time to go to my signal-man’s box.</p> </blockquote> <p>But i need the answer like below </p> <blockquote> <p>....near the top of the deep cutting. I would <strong>extend</strong> my walk for an hour, I said to myself, half an hour....</p> </blockquote> <p>Edit:</p> <blockquote> <p>I need the 10 words after and before the keyword. If the keyword is exists more than once, it will also display 10 words after and before the keyword. Is it clear for you. help me out</p> </blockquote> <p><a href="http://jsfiddle.net/3fTD3/" rel="nofollow">http://jsfiddle.net/3fTD3/</a></p> <p>Thanks in Advance...</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload