0

I wanted to replace a particular text "Dog" in an input box of a webpage to the text "Cat". I am new to jaavascript, so please pardon me for asking this question if I happen to break any forum rules. Thank you in advance. Vicky.

  • 3
    Would be cool if you posted some of your relevant code when you ask a question in here and your attempts to solve the problem, otherwise some ppl might get pissed thinking that you just want them to solve the work for you. By doing that we can see what's your current understanding of the problem at hand and help you get there :) – Chris G May 27 '22 at 15:55
  • 1
    StackOverflow is not a forum, it is more like a dictionary. It seems like you might be asking about two separate things that are already answered. [Here is an answer that explains how to set an input value](https://stackoverflow.com/a/7609144/2647442) and [here is an answer that explains how to replace text](https://stackoverflow.com/a/60836282/2647442) – Marie May 27 '22 at 17:08

3 Answers3

0

You can get an element by it's id and then change it's value

document.getElementById("nameofid").value = "Cat";

MickyDore
  • 146
  • 6
0

you can use replace() function fot that.

const ta = document.querySelector("textarea");

const str = ta.value.replace("dog","cat");
ta.value = str
console.log(ta.value,"change to" ,str)
<textarea>a little dog.</textarea>
Maik Lowrey
  • 15,957
  • 6
  • 40
  • 79
  • Thank you for replying I used the document.querySelectorAll for this problem. But isnt workig. const ta = document.querySelectorAll("[id$='txtValue']"); const str = ta.value.replace("dog","cat"); ta.value = str console.log(ta.value,"change to" ,str) – Vicky Lahkar May 28 '22 at 01:35
  • 1
    You welcome! Do you have multiple textaraes? If you using querySelectorAll you will get a collection of nodes. Means that you must iterate this collection. – Maik Lowrey May 28 '22 at 05:02
-1
<html>
<body>
<h2>Change input box to Cat</h2>
<div id="inputBoxId">Dog</div>
<script>
 document.getElementById("inputBoxId").innerHTML = "Cat";
</script>
</body>
</html>