Check out the problem

Check out the problem
0

#1

I want to replace a word that is given in the input text but it’s not working. check out the replace function.

<textarea id="sentence" placeholder="write a sentence" maxlength="15"></textarea>
	<button onclick="print()">print</button>
	<input id="add" type="text" name="text" maxlength="12">
	<button onclick="add()">Add</button>
	<input id="replace" type="text" name="text">
	<button onclick="replace()">replace</button>

	<script type="text/javascript">
		function print(){
			var a=document.getElementById("sentence").value;
			
			document.getElementById("demo").innerHTML=a;
			
			
		}
		function add(){
				
				var c= document.getElementById("add").value;

				var result=document.getElementById("sentence").value+c;
				document.getElementById("demo").innerHTML=result;
			}
		function replace(){
			var str=document.getElementById("sentence").value+document.getElementById("add").value;
			
			var word= document.getElementById("replace").value;
			
			var result= str.replace(word);

			document.getElementById("demo").innerHTML=result;
		}

			
		
		
	</script>

#2

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

I am not really sure what the purpose of the add function is, but if you want to replace the textarea’s first instance of the word in the input element with id=“add” with the word in the input element with id=“replace”, then you could do something like:

  function replace() {
    var str = document.getElementById("sentence").value;
    var wordToReplace = document.getElementById("add").value;
    var replacement = document.getElementById("replace").value;
    var result = str.replace(wordToReplace, replacement);
    document.getElementById("sentence").value = result;
  }

Also, I was not sure why you were referring to an element with id=“demo”, because I do not see an element with id=“demo” in the code you have above.

If you actually have an element (i.e a div) with id=“demo”, then you could put the replacement sentence in it with:

  function replace() {
    var str = document.getElementById("sentence").value;
    var wordToReplace = document.getElementById("add").value;
    var replacement = document.getElementById("replace").value;
    var result = str.replace(wordToReplace, replacement);
    document.getElementById("demo").innerHTML = result;
  }

#3

sir, I add a textarea where i can write a sentence and the word will be limited and for that i have add another button “add” and with this i can add a word in the end of the sentence and for editing I use a button replace so that i can replace the word. The problem is print and add function is working but i can’t make the replace function workable. If i have a define string then i can replace any word easily but I don’t know what will be my input in the box. so I can’t put the word on the replace bracket and that’s the problem I am facing.


#4

The first code I provided you above will allow you to enter a word into the field located left of the Add button add enter another word into the field located to the left of the replace button. When the replace button is clicked, the text in the textarea will contain the same string with the first word replaced by the second word.

For example, if the following was in the textarea element:

image
and then I put the text “hello” in the first input field and put the text “goodbye” in the second field (like below):

image

Clicking the replace button, will cause the textarea field now have the following:

I guess I am not understanding what you are wanting to do. Alos, I don’t understand what you mean by “define string”.