View Single Post
Old 12-07-2012, 08:19 PM   PM User | #4
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,452
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
cleaned up ht/js, simplified API, removed escape requirement

Code:
<html>
<head>
	<script>// REPLACE VARIABLE VALUE IN TEXTBOX OR TEXTAREA OR DIV BY ANOTHER VALUE
	function doRep(elm, val2replace, change2) {
		var txt = (elm.value||elm.innerHTML),
		  // setting regex with val2replace global and case insensitive
		match = new RegExp(val2replace.replace(/([.*+?^${}()|[\]\/\\])/g, "\\$1"), 'gi'), 
		output = txt.replace(match, change2); // replacing variable string with variable replacement value
		elm[ (elm.select===undefined) ? "innerHTML" : "value" ] = output;
	}
	</script>
</head>
<body>
	<input type=text id=txt1 value="Now is the time for all good men to come to the aid of their country." size=70>
	<br>	<br>

	<textarea id=txt2 cols=40 rows=5>Now is the time for all good men to come to the aid of their country.</textarea>
	<br>	<br>

	<div id=divText>Now is the time for all good men
		<br>to come to the aid of their country.</div>
	<br>

      Find:
	<input type=text id=txt2find >
	<br>Replace:
	<input type=text id=reptext >
	<br>
	<br>
	<br>
	<button onclick=doRep(txt1,txt2find.value,reptext.value)>Textbox: Do it</button>
	<button onclick=doRep(txt2,txt2find.value,reptext.value)>Textarea: Do it</button>
	<button onclick=doRep(divText,txt2find.value,reptext.value)>DIV: Do it</button>
</body>
</html>
tested: IE10/7,FF, CH
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.8% IE9:11.4% IE10:6.5%

Last edited by rnd me; 12-07-2012 at 08:28 PM..
rnd me is offline   Reply With Quote