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 07-18-2012, 08:08 AM   PM User | #1
dennisboys
New to the CF scene

 
Join Date: Jul 2012
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
dennisboys is an unknown quantity at this point
How can I replace the text in an input field?

The replace method only works on a string with the first match substring. In the below example, how can I replace all 'a' with 'b'? Thanks in advance for any help!

Code:
    <script>
function replaceIt(){
	var textStore = document.getElementById("textInput").value;	
	var newText = textStore.replace("a","b");
	alert(newText);
};
	</script>
    

   
<input value="aaaabbbccc" type="text" id="textInput" name="textInput" />
<button onclick="replaceIt()">Replace all the a with b!</button>

Last edited by dennisboys; 07-18-2012 at 08:12 AM..
dennisboys is offline   Reply With Quote
Old 07-18-2012, 08:43 AM   PM User | #2
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
Code:
<script type = "text/javascript">

function replaceIt(){
var textStore = document.getElementById("textInput").value;	
var newText = textStore.replace(/a/gi,"b");  // g means global, i means ignore case
alert(newText);
}
</script>
       
<input value="aaaabbbccc" type="text" id="textInput" name="textInput" />
<button onclick="replaceIt()">Replace all the a with b!</button>

Quizmaster: What is the last letter of the Greek alphabet?
Contestant: Zulu
__________________

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
Users who have thanked Philip M for this post:
dennisboys (07-18-2012)
Old 07-18-2012, 09:23 AM   PM User | #3
dennisboys
New to the CF scene

 
Join Date: Jul 2012
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
dennisboys is an unknown quantity at this point
Thank you Philip!! im new to here and im so glad that i get a response so quickly in the forum.
dennisboys 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 01:50 PM.


Advertisement
Log in to turn off these ads.