...

View Full Version : How can I replace the text in an input field?



dennisboys
07-18-2012, 09:08 AM
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!



<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>

Philip M
07-18-2012, 09:43 AM
<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

dennisboys
07-18-2012, 10:23 AM
Thank you Philip!! im new to here and im so glad that i get a response so quickly in the forum.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum