Select and Replace Text

[Total: 0    Average: 0/5]

This shows how to have selected text replaced.

Head Code:

<SCRIPT language="JavaScript" type="text/javascript"><!--
//This works for IE4+ only!
//

//function replaceall - replace all occurrences of selected string in entire string
//pass it the following variables
// - frm : form name as object
// - resultfld : result field name as object
// - dowhat : text of html tag as text
function replaceall(frm,resultfld,dowhat){
//Full string of entire result field from form
  var FullString = resultfld.value;
//  alert(FullString);
//String containing selected text
  var SelectedString = document.selection.createRange().text;
//  alert(SelectedString);
//Create a regular expression object for the selected string
  var RegExpSelect = new RegExp(SelectedString, "gi")
//String containing updated SelectedString
  var ReplaceString = "<"+dowhat+">"+SelectedString+"</"+dowhat+">";
//  alert(ReplaceString);
//New String with all occurrences of the selected text replaced
  var NewString = FullString.replace (RegExpSelect, ReplaceString);
//  alert(NewString);
//assign the new string value to the result field
  resultfld.value = NewString;
}

//function replacesingle - replace only the selected occurrence of string in entire string
//pass it the following variables
// - frm : form name as object
// - resultfld : result field name as object
// - dowhat : text of html tag as text
function replacesingle(frm,resultfld,dowhat){
//String containing selected text
  var SelectedString = document.selection.createRange().text;
//  alert(SelectedString);
//String containing updated SelectedString
  var ReplaceString = "<"+dowhat+">"+SelectedString+"</"+dowhat+">";
//  alert(ReplaceString);
//assign the replace string value to the selected string
  document.selection.createRange().text = ReplaceString;
}
//-->
</script>

Body Code:

<!-- 

Start Copyright Notice
Please feel free to use and modify this script so as long as this copyright notice remains intact. 

Created by Philip Snow 2001 Copyright: ComputerUSA Web Services, Inc.
All rights reserved.
http://www.cusadesign.com

For LyricsFree.com and it's valuable firends
http://www.lyricsfree.com

Please give us credit where credit is due.

End Copyright Notice
-->
<center>

<h1>Highlight Text then click a button.</h1>
<form name="theform">
<BR>Replace all occurrences of highlighted text in textarea<BR>
<input type="button" name="bold" value="bold" onClick="replaceall(theform,bodytxt,'b')">
<input type="button" name="italic" value="italic" onClick="replaceall(theform,bodytxt,'i')">
<input type="button" name="bullet" value="bullet" onClick="replaceall(theform,bodytxt,'li')">
<BR>
<textarea rows="10" cols="50" NAME="bodytxt" wrap="virtual">This is the teatarea body.  Highlight some text in the body section and then click one of the buttons above.  Did you know that the body was here.  Now you do know exactly where the body is.  We hope you enjoy this free javascript example.</textarea>
<BR><BR>Replace only single selection<BR>
<input type="button" name="bold" value="bold" onClick="replacesingle(theform,bodytxt2,'b')">
<input type="button" name="italic" value="italic" onClick="replacesingle(theform,bodytxt2,'i')">
<input type="button" name="bullet" value="bullet" onClick="replacesingle(theform,bodytxt2,'li')">
<BR>
<textarea rows="10" cols="50" NAME="bodytxt2" wrap="virtual">This is the teatarea body.  Highlight some text in the body section and then click one of the buttons above.  Did you know that the body was here.  Now you do know exactly where the body is.  We hope you enjoy this free javascript example.</textarea>
</form>

</center>

Times Viewed: 6

Leave a Reply

Your email address will not be published. Required fields are marked *