Advertisement
Andres_Chandia

java function to save textarea content retrieved from a file

Nov 5th, 2014
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.96 KB | None | 0 0
  1. file: index.php
  2. <head>
  3. <script src="jquery.js" type="text/javascript"></script>
  4. <script src="script.js" type="text/javascript"></script>
  5. </head>
  6. <input placeholder="Search" type="text" id="termSearch" name="termSearch" />
  7. <input placeholder="Replace" type="text" id="termReplace" name="termReplace" />
  8. <label for="caseSensitive">Case Sensitive</label><input type="checkbox" name="caseSensitive" id="caseSensitive" />
  9. <a href="#" onclick="SAR.find(); return false;" id="find"><input type="submit" value="Find"></a>
  10. <a href="#" onclick="SAR.findAndReplace(); return false;" id="findAndReplace"><input type="submit" value="Find & Replace"></a>
  11. <a href="#" onclick="SAR.replaceAll(); return false;" id="replaceAll"><input type="submit" value="Replace All"></a>
  12.  
  13. ******** This is the function I would like to add************
  14. <a href="#" onclick="SAR.save(); return false;"><input type="submit" value="Save changes"></a>
  15. **************************************************************
  16.  
  17. <textarea id="txtArea" rows="26"><?php echo $todisplay; ?></textarea>
  18.  
  19. file: script.js
  20. var SAR = {};
  21.  
  22. SAR.find = function(){
  23. // collect variables
  24. var txt = $("#txtArea").val();
  25. var strSearchTerm = $("#termSearch").val();
  26. var isCaseSensitive = ($("#caseSensitive").attr('checked') == 'checked') ? true : false;
  27.  
  28. // make text lowercase if search is supposed to be case insensitive
  29. if(isCaseSensitive == false){
  30. txt = txt.toLowerCase();
  31. strSearchTerm = strSearchTerm.toLowerCase();
  32. }
  33.  
  34. // find next index of searchterm, starting from current cursor position
  35. var cursorPos = ($("#txtArea").getCursorPosEnd());
  36. var termPos = txt.indexOf(strSearchTerm, cursorPos);
  37.  
  38. // if found, select it
  39. if(termPos != -1){
  40. $("#txtArea").selectRange(termPos, termPos+strSearchTerm.length);
  41. }else{
  42. // not found from cursor pos, so start from beginning
  43. termPos = txt.indexOf(strSearchTerm);
  44. if(termPos != -1){
  45. $("#txtArea").selectRange(termPos, termPos+strSearchTerm.length);
  46. }else{
  47. alert("not found");
  48. }
  49. }
  50. };
  51.  
  52. SAR.findAndReplace = function(){
  53. // collect variables
  54. var origTxt = $("#txtArea").val(); // needed for text replacement
  55. var txt = $("#txtArea").val(); // duplicate needed for case insensitive search
  56. var strSearchTerm = $("#termSearch").val();
  57. var strReplaceWith = $("#termReplace").val();
  58. var isCaseSensitive = ($("#caseSensitive").attr('checked') == 'checked') ? true : false;
  59. var termPos;
  60.  
  61. // make text lowercase if search is supposed to be case insensitive
  62. if(isCaseSensitive == false){
  63. txt = txt.toLowerCase();
  64. strSearchTerm = strSearchTerm.toLowerCase();
  65. }
  66.  
  67. // find next index of searchterm, starting from current cursor position
  68. var cursorPos = ($("#txtArea").getCursorPosEnd());
  69. var termPos = txt.indexOf(strSearchTerm, cursorPos);
  70. var newText = '';
  71.  
  72. // if found, replace it, then select it
  73. if(termPos != -1){
  74. newText = origTxt.substring(0, termPos) + strReplaceWith + origTxt.substring(termPos+strSearchTerm.length, origTxt.length)
  75. $("#txtArea").val(newText);
  76. $("#txtArea").selectRange(termPos, termPos+strReplaceWith.length);
  77. }else{
  78. // not found from cursor pos, so start from beginning
  79. termPos = txt.indexOf(strSearchTerm);
  80. if(termPos != -1){
  81. newText = origTxt.substring(0, termPos) + strReplaceWith + origTxt.substring(termPos+strSearchTerm.length, origTxt.length)
  82. $("#txtArea").val(newText);
  83. $("#txtArea").selectRange(termPos, termPos+strReplaceWith.length);
  84. }else{
  85. alert("not found");
  86. }
  87. }
  88. };
  89.  
  90. SAR.replaceAll = function(){
  91. // collect variables
  92. var origTxt = $("#txtArea").val(); // needed for text replacement
  93. var txt = $("#txtArea").val(); // duplicate needed for case insensitive search
  94. var strSearchTerm = $("#termSearch").val();
  95. var strReplaceWith = $("#termReplace").val();
  96. var isCaseSensitive = ($("#caseSensitive").attr('checked') == 'checked') ? true : false;
  97.  
  98. // make text lowercase if search is supposed to be case insensitive
  99. if(isCaseSensitive == false){
  100. txt = txt.toLowerCase();
  101. strSearchTerm = strSearchTerm.toLowerCase();
  102. }
  103.  
  104. // find all occurances of search string
  105. var matches = [];
  106. var pos = txt.indexOf(strSearchTerm);
  107. while(pos > -1) {
  108. matches.push(pos);
  109. pos = txt.indexOf(strSearchTerm, pos+1);
  110. }
  111.  
  112. for (var match in matches){
  113. SAR.findAndReplace();
  114. }
  115. };
  116.  
  117.  
  118. /* TWO UTILITY FUNCTIONS YOU WILL NEED */
  119. $.fn.selectRange = function(start, end) {
  120. return this.each(function() {
  121. if(this.setSelectionRange) {
  122. this.focus();
  123. this.setSelectionRange(start, end);
  124. } else if(this.createTextRange) {
  125. var range = this.createTextRange();
  126. range.collapse(true);
  127. range.moveEnd('character', end);
  128. range.moveStart('character', start);
  129. range.select();
  130. }
  131. });
  132. };
  133.  
  134. $.fn.getCursorPosEnd = function() {
  135. var pos = 0;
  136. var input = this.get(0);
  137. // IE Support
  138. if (document.selection) {
  139. input.focus();
  140. var sel = document.selection.createRange();
  141. pos = sel.text.length;
  142. }
  143. // Firefox support
  144. else if (input.selectionStart || input.selectionStart == '0')
  145. pos = input.selectionEnd;
  146. return pos;
  147. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement