0

https://jsfiddle.net/bob90937/2yw3s376/ I am able to one sentence in one content as u can see from the following jsp,html:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<p id="sentence">a paragraph</p>
<p id="content">And this here is inside a paragraph , about paragliders. happy ending.</p>

css:

.highlighted {
  background: yellow
}

javascript:

$(document).ready(function() {

  var wordText = $('#sentence').text();
  var rootText = $('#content').text();
  var rootNode = document.getElementById('content');

  highlightWord(rootText, wordText);

  function highlightWord(rootText, wordText) {

    textNodesUnder(rootNode).forEach(highlightWords);

    function textNodesUnder(rootNode) {
      var walk = document.createTreeWalker(rootNode, NodeFilter.SHOW_TEXT, null, false);
      var text = [],
        node;
      while (node = walk.nextNode()) text.push(node);

      return text;
    }

    function highlightWords(n) {
      for (var i;
        (i = n.nodeValue.indexOf(wordText, i)) > -1; n = after) {
        var after = n.splitText(i + wordText.length);
        var highlighted = n.splitText(i);
        var span = document.createElement('span');
        span.className = 'highlighted';
        span.appendChild(highlighted);
        after.parentNode.insertBefore(span, after);
      }
    }
  }

});

IF i have multiple sentence and content and I save it in the table. How to change the code so that the multiple sentence and content can be highlighted??? enter image description here

..

  <table class="gridtable" border="1"> 
<tbody>
<tr>
    <th>sentence</th>
    <th>content</th>

  </tr>

<tr>
<td id="sentence">a paragraph</td>
<td id="content">And this here  is inside a paragraph , about paragliders.happy ending.</td> 
<tr>
<td id="sentence">unless they were granted</td>
<td id="content">All Singaporean children of school-going age are required to regularly attend a national primary school, unless they were granted an exemption due to physical or intellectual disabilities.</td> 
<tr>
<td id="sentence">efforts to</td>
<td id="content">MOE said that the change is part of the Government ongoing efforts to build a more inclusive society.</td> 
</tr>
</table> 

https://jsfiddle.net/bob90937/2yw3s376/ the link

HiPownedBi
  • 195
  • 2
  • 8
  • 18
  • Why don't you use [mark.js](https://markjs.io/)? – dude Nov 04 '16 at 07:12
  • Also, please provide an example code that can run on e.g. [jsfiddle](https://jsfiddle.net)! Your code contains template things and therefore we can't test it. – dude Nov 04 '16 at 07:13
  • Hey. is your sentences are dynamic or static? – Darence30 Nov 04 '16 at 07:17
  • @Darence30 the sentences stored in the array. the sentence is alway part of the content – HiPownedBi Nov 04 '16 at 07:19
  • https://jsfiddle.net/bob90937/2yw3s376/ this link – HiPownedBi Nov 04 '16 at 07:25
  • @dude https://jsfiddle.net/bob90937/2yw3s376/ – HiPownedBi Nov 04 '16 at 07:25
  • ID stand for identification, so basicly you can only have one ID. Here I see 3 `sentence` and 3 `content`. When JQuery search for an element by his ID, he will take the first one (I guess) he found. – AxelH Nov 04 '16 at 07:39
  • @HiPownedBi Agai, you're reinverting the wheel. Highlighting things has been discussed dozens of times here on SO. What's the point for not just using e.g. [mark.js](https://markjs.io/)? – dude Nov 04 '16 at 07:48
  • Possible duplicate of [Highlight a word with jQuery](http://stackoverflow.com/questions/119441/highlight-a-word-with-jquery) – dude Nov 04 '16 at 07:48
  • @dude, on the context I agree but on the code provided, these are totally different subject. But nice answer about mark.js ;) – AxelH Nov 04 '16 at 08:01

1 Answers1

0

You have an ID clash. You gave multiple elements ID's of content and sentence. Your Javascript is only retrieving the first one it finds.

ppovoski
  • 4,553
  • 5
  • 22
  • 28