I would like to find and replace the text in my textbox.
this is my script
<script>
function findnReplace() {
var str = document.getElementById("source").value;
var find = document.getElementById("find").value;
var replace = document.getElementById("replace").value;
var resultString = str.replace(find, replace);
var numreplace = new RegExp(find, 'g');
document.getElementById("source").innerHTML = resultString;
//find the number of words found and replaced
var num = str.match(numreplace).length;
if (num == 0) {
var no = "No words are replaced.";
document.getElementById("num").innerHTML = no;
} else {
var n = num + " word(s) replaced.";
document.getElementById("num").innerHTML = n;
}
}
</script>
and here is my html code
<html>
<body>
<table>
<textarea name="text" id="source" rows="3" cols="20" required>Hello Testing
</textarea><br><br>
<tr>
<td>Find:</td>
<td>
<input type="text" id="find" name="find" onkeyup="replaceNum()" size="30">
</td>
</tr>
<tr>
<td>Replace:</td>
<td>
<input type="text" id="replace" name="replace" onkeyup="replaceNum()" size="30">
</td>
</tr>
</table>
<input id="findnReplaceButton" type="button" value="Find & Replace"
onclick="findnReplace()" title="Fill in both textbox"/>
<span id="num"></span>
</table>
</body>
</html>
expected result:
however, this is what i am getting:
ALTHOUGH it says "3words replaced" but the text in the textbox didnt get replaced.

