views:

430

answers:

5

Hi, is it posible to replace a character at a specific position within a textarea or textbox? I can work out the position using indexOf() but knowing this how can i actually replace that particular character?

+1  A: 

You can use substr or substring to do that (notice the different semantics of the second parameter):

var str = 'foobar';
alert(str.substr(3, 3));    // bar
alert(str.substring(3, 6)); // bar
Gumbo
A: 

Use replace function:

<script type="text/javascript">

var str="Visit Microsoft!";
document.write(str.replace("Microsoft", "W3Schools"));

</script> 
alemjerus
This will only replace the 1st occurrence, what if he wants to replace the second, or third?
Andy E
The replace function is not the answer to the question asked here, "replace a character at a specific position".
Lars
+2  A: 
<html>
<head>
<script>
function modText()
{
 var tb = document.getElementById("mtb");
 var indexToReplace = 1;
 var stringToPutIn= "!";
 var temp = tb.value;
 var startString = temp.substr(0, indexToReplace);
 var endString = temp.substring(indexToReplace+1);
 tb.value = startString+stringToPutIn+endString;
}
</script>
</head>
<body>
<input type="text" id="mtb" /><br />
<input type="button" onclick="modText();">
</body>
</html>

The replace function will NOT work because you might have the same letter coming before the letter you wish to replace.

Not the above function only works if you're replacing one and only one character.

ItzWarty
cheers. Worked perfectly.
David
A: 

You could do something like this using replace

<input id="myTxtbox" type="textbox">

<script type="text/javascript">
    var txtbox = document.getElementByID("myTxtbox");
    document.write(txtbox.value.replace("To Replace", "With This"));
</script> 
lillq
This will replace the first occurance of the string. In most cases this will work, but the logic isn't correct. For example, lets say you have string of "apple cherry apple cherry"if you called replace("apple", "orange"), you would only be able to replace the first apple [resulting in "orange cherry apple cherry"] Of course, you could use regexp's global... but that still wouldn't allow you to replace the nth instance of "apple"
ItzWarty
+1  A: 

Just use the built-in methods of JavaScript strings. Assuming you have a text area in a variable textArea:

var textArea = document.getElementById("yourTextArea");

... the following creates a nice, generic, reusable string splicing function analogous to Array's splice method that you can then use to update the textarea's value:

function spliceString(str, start, count, stringToInsert) {
  return str.substring(0, start) + stringToInsert + str.substr(start + count);
}

textArea.value = spliceString(textArea.value, charIndex, 1, "**NEW BIT**");

An alternative would be to create a splice method of all strings by augmenting String's prototype:

String.prototype.splice = function(start, count, stringToInsert) {
  return this.substring(0, start) + stringToInsert + this.substr(start + count);
};

textArea.value = textArea.value.splice(charIndex, 1, "**NEW BIT**");
Tim Down
thank you :) this helped me a lot
GerManson