Go Back   CodingForums.com > :: Client side development > JavaScript programming > Post a JavaScript

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-07-2012, 04:32 PM   PM User | #1
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Replace variable value in textbox or textarea or div by another value

Code:
<html>
<head>

<script type="text/javascript">

// REPLACE VARIABLE VALUE IN TEXTBOX OR TEXTAREA OR DIV BY ANOTHER VALUE

function doRep(IDS, val2replace, change2, elem) {
if (elem == "T") {
var txt = document.getElementById(IDS).value; // string to be analysed
}
if (elem == "D") {
var txt = document.getElementById(IDS).innerHTML; // string to be analyzed
}
var match = new RegExp(val2replace, 'gi');   // setting regex with val2replace global and case insensitive
var output = txt.replace(match, change2);     // replacing variable string with variable replacement value
if (elem == "T") {
document.getElementById(IDS).value=output;
}
if (elem == "D") {
document.getElementById(IDS).innerHTML=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" value=""><br>
Replace: <input type="text" id="reptext" value=""><br>
<br><br>
<button
 onclick="doRep('txt1',document.getElementById('txt2find').value,document.getElementById('reptext').value, 'T')">
 Textbox: Do it</button>
<button
 onclick="doRep('txt2',document.getElementById('txt2find').value,document.getElementById('reptext').value, 'T')">
 Textarea: Do it</button>
<button
 onclick="doRep('divText',document.getElementById('txt2find').value,document.getElementById('reptext').value, 'D')">
 DIV: Do it</button>


</body>
</html>
<br><br><br>
You need to escape the 11 following characters (often called "metacharacters") IN THE FIND STRING with a backslash : <br>
the opening square bracket [, <br>
the backslash \, <br>
the caret ^, <br>
the dollar sign $, <br>
the period or dot ., <br>
the vertical bar or pipe symbol |, <br>
the question mark ?, <br>
the asterisk or star *, <br>
the plus sign +, <br>
the opening round bracket ( <br>
and the closing round bracket ). <br>
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 12-07-2012, 06:54 PM   PM User | #2
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
Quote:
Originally Posted by Philip M View Post

You need to escape the 11 following characters (often called "metacharacters") IN THE FIND STRING with a backslash : <br>
the opening square bracket [, <br>
the backslash \, <br>
the caret ^, <br>
the dollar sign $, <br>
the period or dot ., <br>
the vertical bar or pipe symbol |, <br>
the question mark ?, <br>
the asterisk or star *, <br>
the plus sign +, <br>
the opening round bracket ( <br>
and the closing round bracket ). <br>
why don't you do that for us?
__________________
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%
rnd me is offline   Reply With Quote
Old 12-07-2012, 07:05 PM   PM User | #3
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Just a suggestion to use:

Code:
var oIDS = document.getElementById(IDS);
..but it's not significant .

Quote:
why don't you do that for us?
..that was my first thought as well
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
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
Old 12-07-2012, 08:41 PM   PM User | #5
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
I don't want to appear pedantic but.. DOCTYPE and title..

I would, personally, use quotes - particularly for onclick.

Added: I might also prefer:

Code:
elm[ (elm.value) ? "value" : "innerHTML" ] = output;
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS

Last edited by AndrewGSW; 12-07-2012 at 08:49 PM..
AndrewGSW is offline   Reply With Quote
Old 12-07-2012, 09:29 PM   PM User | #6
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
Quote:
Originally Posted by AndrewGSW View Post
Added: I might also prefer:

Code:
elm[ (elm.value) ? "value" : "innerHTML" ] = output;
were showing off js here, not html. the function is fine. i wanted to remind folks of html options as a side-effect.

if you simply check for .value, you can erroneously hit on <li> and possibly others.
also, that misses empty inputs.

anything a user can edit with the keyboard should have a select method attached, custom controls included.
__________________
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 09:32 PM..
rnd me is offline   Reply With Quote
Old 12-07-2012, 09:32 PM   PM User | #7
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
@rnd me. Thank you.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Old 12-07-2012, 10:01 PM   PM User | #8
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,763
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
Since when did this section of the forum become a question rather than solution threads?
jmrker is offline   Reply With Quote
Old 12-07-2012, 10:31 PM   PM User | #9
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
Quote:
Originally Posted by jmrker View Post
Since when did this section of the forum become a question rather than solution threads?
you're the only one asking questions here... hehe
__________________
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%
rnd me is offline   Reply With Quote
Old 12-08-2012, 12:16 AM   PM User | #10
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,763
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
Arrow

Quote:
Originally Posted by rnd me View Post
you're the only one asking questions here... hehe
Story of my life.
I'm not part of the solution, so I must be part of the problem!
jmrker is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:21 AM.


Advertisement
Log in to turn off these ads.