1

This is the code I'm using but this replace only the single word, how can I replace multiple words. Input: Good Morning, this is jack here. Output: Good Evening, this is stack here.

function deleteText() {
  var my = document.getElementById('mytextarea');
  var myvalue = my.value;
  my.value = myvalue.replace("Morning", "Evening");
}
<textarea style="text-transform: uppercase; white-space: normal" cols="50" rows="3" id="mytextarea">Good Morning, this is jack here.
</textarea>
<input type="button" value="Delete" onClick="deleteText()">
  • would you like to replace the whole content? because you can do that by just assigning a new value – Chris Jun 27 '21 at 14:29
  • no, only the selective words. –  Jun 27 '21 at 14:30
  • Why not make another statement like my.value = myvalue.replace("Morning", "Evening"); but with the new content? the first input is what will be replaced and the second is what it will be replaced by. Is that what you're looking for? – Chris Jun 27 '21 at 14:31
  • You are actually assigning a new value with `my.value = ...` but the answer is just to do all the mutations to the string you want before reassigning it to the input.value – pilchard Jun 27 '21 at 14:32
  • 1
    [codepen](https://codepen.io/kumarmasterpraveen/pen/YzVKOdo), May be this is what you are looking for... – DecPK Jun 27 '21 at 14:35
  • function deleteText() { var my = document.getElementById('mytextarea'); var myvalue = my.value; myvalue = myvalue.replace("Morning", "Evening"); my.value = myvalue; }; This code works, but there might be better solutions out there... :) – John Carlson Jun 27 '21 at 15:02

0 Answers0