Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Highlight user selected text using javascript
- //-------------------------------------------------------
- // Name: doHighlight()
- // Find all occurences of the search term in the given text,
- // and add some "highlight" tags to them (we're not using a
- // regular expression search, because we want to filter out
- // matches that occur within HTML tags and script blocks, so
- // we have to do a little extra validation)
- //-------------------------------------------------------
- function doHighlight(bodyText, searchTerm, highlightStartTag, highlightEndTag)
- {
- var newText = "";
- var i = -1;
- var lcSearchTerm = searchTerm.toLowerCase();
- var lcBodyText = bodyText.toLowerCase();
- var temp = 0;
- var counter = 0;
- //********************************************************************
- //********************************************************************
- //********************************************************************
- //Here is the ID of the <p> element where our text was selected
- alert(textId);
- //Here is the text of the ID of the <p> element we selected
- alert(document.getElementById(textId).innerHTML);
- //********************************************************************
- //********************************************************************
- //********************************************************************
- while (bodyText.length > 0)
- {
- i = lcBodyText.indexOf(lcSearchTerm, i++);
- if (i < 0)
- {
- newText += bodyText;
- bodyText = "";
- }
- else
- {
- // skip anything inside an HTML tag
- if (bodyText.lastIndexOf(">", i) >= bodyText.lastIndexOf("<", i))
- {
- // skip anything inside a <script> block
- if (lcBodyText.lastIndexOf("/script>", i) >= lcBodyText.lastIndexOf("<script", i))
- {
- //********************************************************************
- //********************************************************************
- //********************************************************************
- // Writes the document til it reaches the search term, then adds highlightStartTag then the search term then highlightEndTag
- // Then writes more of the document until the search term is reached again
- // Needs to search for the term based on the id="" given to the <p> element
- // Then add the highlightStartTag and highlightEndTag
- newText += bodyText.substring(0, i) + highlightStartTag + bodyText.substr(i, searchTerm.length) + highlightEndTag;
- bodyText = bodyText.substr(i + searchTerm.length);
- lcBodyText = bodyText.toLowerCase();
- i = +1;
- //********************************************************************
- //********************************************************************
- //********************************************************************
- }
- }
- }
- //break;
- }
- //newText += bodyText;
- return newText;
- }
- //-------------------------------------------------------
Add Comment
Please, Sign In to add comment