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

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 09-07-2012, 10:58 AM   PM User | #1
abcabc
New to the CF scene

 
Join Date: Sep 2012
Posts: 5
Thanks: 2
Thanked 0 Times in 0 Posts
abcabc is an unknown quantity at this point
replace str in text area

Hi im trying to replace some text in a text area however it cant seems to pick up the text from inside the textarea. any help is appreciated

<html>
<body>

<p>Click button:</p>

<textarea id="zebra">Type here!</textarea>

<button onclick="myFunction()">Click here</button>

<script>
function myFunction()
{
var str=document.getElementById("zebra").innerHTML;
var n=str.replace("zebra","horse");
document.getElementById("zebra").innerHTML=n;
}
</script>

</body>
</html>
abcabc is offline   Reply With Quote
Old 09-07-2012, 11:04 AM   PM User | #2
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,680
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
Change .innerHTML to .value
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Users who have thanked abduraooft for this post:
abcabc (09-07-2012)
Old 09-07-2012, 11:11 AM   PM User | #3
abcabc
New to the CF scene

 
Join Date: Sep 2012
Posts: 5
Thanks: 2
Thanked 0 Times in 0 Posts
abcabc is an unknown quantity at this point
thank you it works !
abcabc is offline   Reply With Quote
Old 09-07-2012, 11:15 AM   PM User | #4
abcabc
New to the CF scene

 
Join Date: Sep 2012
Posts: 5
Thanks: 2
Thanked 0 Times in 0 Posts
abcabc is an unknown quantity at this point
however the str zebra might be repeated many times throughout the text. Is there anyway to have it replace all with a click instead of having the user to press the button multiple times ? thank you in advance
abcabc is offline   Reply With Quote
Old 09-07-2012, 12:13 PM   PM User | #5
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by abcabc View Post
however the str zebra might be repeated many times throughout the text. Is there anyway to have it replace all with a click instead of having the user to press the button multiple times ? thank you in advance
Code:
var n=str.replace(/zebra/gi,"horse");
__________________

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 09-07-2012, 01:03 PM   PM User | #6
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,455
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
the fastest way i know uses strings to do universal replacements:
Code:
var n=str.split("zebra").join("horse");
probably doesn't matter for littlethings, but on big loops or when making lots of replacements, it's noticeably faster than regexp...
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
rnd me is offline   Reply With Quote
Old 09-07-2012, 02:53 PM   PM User | #7
DaveyErwin
Regular Coder

 
Join Date: Aug 2010
Posts: 809
Thanks: 12
Thanked 168 Times in 166 Posts
DaveyErwin is on a distinguished road
Of course
var n=str.split("zebra").join("horse");
won't work neither will
var n=str.replace(/zebra/gi,"horse");
IF you do not want fazebrain to
become fahorsein.
DaveyErwin is offline   Reply With Quote
Old 09-07-2012, 03:25 PM   PM User | #8
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
He wanted all instances of zebra replaced by horse.

But if only whole words are wanted:-

Code:
var n=str.replace(/\bzebra\b/gi,"horse");
n = n.replace(/\bmouse\b/gi,"cat");
n = n.replace(/\bmice\b/gi,"cats");
__________________

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.

Last edited by Philip M; 09-07-2012 at 03:32 PM..
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
abcabc (09-08-2012)
Old 09-08-2012, 04:33 AM   PM User | #9
abcabc
New to the CF scene

 
Join Date: Sep 2012
Posts: 5
Thanks: 2
Thanked 0 Times in 0 Posts
abcabc is an unknown quantity at this point
Thank you everyone for their input. I managed to get what I wanted.
abcabc is offline   Reply With Quote
Old 09-08-2012, 05:26 PM   PM User | #10
abcabc
New to the CF scene

 
Join Date: Sep 2012
Posts: 5
Thanks: 2
Thanked 0 Times in 0 Posts
abcabc is an unknown quantity at this point
Is it possible for the code

var n=str.replace(/\bII\b/gi,"\n");
n = n.replace(/\bmouse\b/gi,"cat");

to work with a symbol specifically the 2 brackets. ))

thank you again
abcabc is offline   Reply With Quote
Old 09-08-2012, 06:27 PM   PM User | #11
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
You need to escape (with a backslash) the following META characters in a regex pettern so thay are interpreted as literals and not control characters:-
^ $ \ / ( ) | ? + * [ ] { } . (but not , )

So:-


Code:
<script type = "text/javascript">

var str = "abc))xyz))";
str = str.replace(/\)/g,"something")
alert (str);

</script>
__________________

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.

Last edited by Philip M; 09-08-2012 at 06:29 PM..
Philip M 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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:13 PM.


Advertisement
Log in to turn off these ads.