oninput
is not a right choice since it will change for every key press.
use
onchange
instead , however you have to validate the letter '
d
' which might contain in any word of the input.
you have to use
/d/g
for global replacement.
<html>
<body>
<textarea id="ta" onchange="replacetext()"></textarea>
<script>
function replacetext() {
var ta = document.getElementById("ta");
ta.value = ta.value.replace(/d/g, "document");
ta.selectionStart = ta.selectionEnd = ta.value.length;
}
</script>
</body>
</html>