daily pastebin goal
86%
SHARE
TWEET

Untitled

a guest Jan 12th, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Highlight user selected text using javascript
  2. //-------------------------------------------------------
  3. // Name: doHighlight()
  4. // Find all occurences of the search term in the given text,
  5. // and add some "highlight" tags to them (we're not using a
  6. // regular expression search, because we want to filter out
  7. // matches that occur within HTML tags and script blocks, so
  8. // we have to do a little extra validation)
  9. //-------------------------------------------------------
  10. function doHighlight(bodyText, searchTerm, highlightStartTag, highlightEndTag)
  11. {
  12.     var newText = "";
  13.     var i = -1;
  14.     var lcSearchTerm = searchTerm.toLowerCase();
  15.     var lcBodyText = bodyText.toLowerCase();
  16.     var temp = 0;
  17.     var counter = 0;
  18.  
  19. //********************************************************************
  20. //********************************************************************
  21. //********************************************************************
  22. //Here is the ID of the <p> element where our text was selected
  23.     alert(textId);
  24. //Here is the text of the ID of the <p> element we selected
  25.      alert(document.getElementById(textId).innerHTML);
  26. //********************************************************************
  27. //********************************************************************
  28. //********************************************************************
  29.  
  30.     while (bodyText.length > 0)
  31.     {
  32.         i = lcBodyText.indexOf(lcSearchTerm, i++);
  33.  
  34.         if (i < 0)
  35.     {
  36.         newText += bodyText;
  37.         bodyText = "";
  38.     }
  39.     else
  40.     {
  41.         // skip anything inside an HTML tag
  42.          if (bodyText.lastIndexOf(">", i) >= bodyText.lastIndexOf("<", i))
  43.         {
  44.             // skip anything inside a <script> block
  45.             if (lcBodyText.lastIndexOf("/script>", i) >= lcBodyText.lastIndexOf("<script", i))
  46.             {  
  47. //********************************************************************
  48. //********************************************************************
  49. //********************************************************************
  50. // Writes the document til it reaches the search term, then adds highlightStartTag then the search term then highlightEndTag
  51. // Then writes more of the document until the search term is reached again
  52. // Needs to search for the term based on the id="" given to the <p> element
  53. // Then add the highlightStartTag and highlightEndTag
  54.                 newText += bodyText.substring(0, i) + highlightStartTag + bodyText.substr(i, searchTerm.length) + highlightEndTag;
  55.                 bodyText = bodyText.substr(i + searchTerm.length);
  56.                 lcBodyText = bodyText.toLowerCase();
  57.                 i = +1;
  58. //********************************************************************
  59. //********************************************************************
  60. //********************************************************************
  61.                 }
  62.             }
  63.         }
  64.         //break;
  65.     }
  66.     //newText += bodyText;
  67.     return newText;
  68. }
  69. //-------------------------------------------------------
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top