Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I updated my earlier answer and finished the search and replace functionality based on the direction my earlier post outlined. I tested this in Chrome 14, IE8 and Firefox 3.6. </p> <p><strong>Find</strong>: will select the next instance of the searchterm.</p> <p><strong>Find/Replace</strong>: will replace the next occurrence of the search string and select the replacement</p> <p><strong>Replace All:</strong> will replace all occurences and select the piece of text that has been replaced last</p> <p>Hope this is now, what you were looking for. You should definitely be able to go from here and style this or make a proper class out of this...</p> <pre><code>&lt;script src="jquery.js" type="text/javascript"&gt;&lt;/script&gt; &lt;textarea id="txtArea" style="width: 300px; height:100px"&gt; This is a test. A test, i say. The word TEST is mentioned three times. &lt;/textarea&gt; &lt;p&gt; &lt;label for="termSearch"&gt;Search&lt;/label&gt; &lt;input type="text" id="termSearch" name="termSearch" value="test" /&gt;&lt;br/&gt; &lt;label for="termReplace"&gt;Replace&lt;/label&gt; &lt;input type="text" id="termReplace" name="termReplace" value="solution" /&gt;&lt;br/&gt; &lt;label for="caseSensitive"&gt;Case Sensitive&lt;/label&gt; &lt;input type="checkbox" name="caseSensitive" id="caseSensitive" /&gt;&lt;br/&gt; &lt;a href="#" onclick="SAR.find(); return false;" id="find"&gt;FIND&lt;/a&gt;&lt;br/&gt; &lt;a href="#" onclick="SAR.findAndReplace(); return false;" id="findAndReplace"&gt;FIND/REPLACE&lt;/a&gt;&lt;br/&gt; &lt;a href="#" onclick="SAR.replaceAll(); return false;" id="replaceAll"&gt;REPLACE ALL&lt;/a&gt;&lt;br/&gt; &lt;/p&gt; &lt;script type="text/javascript"&gt; var SAR = {}; SAR.find = function(){ // collect variables var txt = $("#txtArea").val(); var strSearchTerm = $("#termSearch").val(); var isCaseSensitive = ($("#caseSensitive").attr('checked') == 'checked') ? true : false; // make text lowercase if search is supposed to be case insensitive if(isCaseSensitive == false){ txt = txt.toLowerCase(); strSearchTerm = strSearchTerm.toLowerCase(); } // find next index of searchterm, starting from current cursor position var cursorPos = ($("#txtArea").getCursorPosEnd()); var termPos = txt.indexOf(strSearchTerm, cursorPos); // if found, select it if(termPos != -1){ $("#txtArea").selectRange(termPos, termPos+strSearchTerm.length); }else{ // not found from cursor pos, so start from beginning termPos = txt.indexOf(strSearchTerm); if(termPos != -1){ $("#txtArea").selectRange(termPos, termPos+strSearchTerm.length); }else{ alert("not found"); } } }; SAR.findAndReplace = function(){ // collect variables var origTxt = $("#txtArea").val(); // needed for text replacement var txt = $("#txtArea").val(); // duplicate needed for case insensitive search var strSearchTerm = $("#termSearch").val(); var strReplaceWith = $("#termReplace").val(); var isCaseSensitive = ($("#caseSensitive").attr('checked') == 'checked') ? true : false; var termPos; // make text lowercase if search is supposed to be case insensitive if(isCaseSensitive == false){ txt = txt.toLowerCase(); strSearchTerm = strSearchTerm.toLowerCase(); } // find next index of searchterm, starting from current cursor position var cursorPos = ($("#txtArea").getCursorPosEnd()); var termPos = txt.indexOf(strSearchTerm, cursorPos); var newText = ''; // if found, replace it, then select it if(termPos != -1){ newText = origTxt.substring(0, termPos) + strReplaceWith + origTxt.substring(termPos+strSearchTerm.length, origTxt.length) $("#txtArea").val(newText); $("#txtArea").selectRange(termPos, termPos+strReplaceWith.length); }else{ // not found from cursor pos, so start from beginning termPos = txt.indexOf(strSearchTerm); if(termPos != -1){ newText = origTxt.substring(0, termPos) + strReplaceWith + origTxt.substring(termPos+strSearchTerm.length, origTxt.length) $("#txtArea").val(newText); $("#txtArea").selectRange(termPos, termPos+strReplaceWith.length); }else{ alert("not found"); } } }; SAR.replaceAll = function(){ // collect variables var origTxt = $("#txtArea").val(); // needed for text replacement var txt = $("#txtArea").val(); // duplicate needed for case insensitive search var strSearchTerm = $("#termSearch").val(); var strReplaceWith = $("#termReplace").val(); var isCaseSensitive = ($("#caseSensitive").attr('checked') == 'checked') ? true : false; // make text lowercase if search is supposed to be case insensitive if(isCaseSensitive == false){ txt = txt.toLowerCase(); strSearchTerm = strSearchTerm.toLowerCase(); } // find all occurances of search string var matches = []; var pos = txt.indexOf(strSearchTerm); while(pos &gt; -1) { matches.push(pos); pos = txt.indexOf(strSearchTerm, pos+1); } for (var match in matches){ SAR.findAndReplace(); } }; /* TWO UTILITY FUNCTIONS YOU WILL NEED */ $.fn.selectRange = function(start, end) { return this.each(function() { if(this.setSelectionRange) { this.focus(); this.setSelectionRange(start, end); } else if(this.createTextRange) { var range = this.createTextRange(); range.collapse(true); range.moveEnd('character', end); range.moveStart('character', start); range.select(); } }); }; $.fn.getCursorPosEnd = function() { var pos = 0; var input = this.get(0); // IE Support if (document.selection) { input.focus(); var sel = document.selection.createRange(); pos = sel.text.length; } // Firefox support else if (input.selectionStart || input.selectionStart == '0') pos = input.selectionEnd; return pos; }; &lt;/script&gt; </code></pre>
 

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