I want to create a button that allows me to change ANY word or PHARSE in a html body. I want to be able to INPUT a word I want to change, and INPUT a word to be changed to. By using an execution button the changes should be applied.
Hope somebody will know how to do it : )
There is another explanation of the task in HTML text.
function changeText () {
var x = document.getElementById("input1").value;
var y = document.getElementById("input2").value;
var textnodes, node, s;
textnodes = document.evaluate("//text()", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
for (var i = 0; i < textnodes.snapshotLength; i++) {
node = textnodes.snapshotItem(i);
s = node.data;
s = s.replace(x, y);
node.data = s;
}
/*
This is what i found and I do not understand it. It changes only one word at time (yes i know there is something like /"abc.."/g, g-global and everything changes but it doesn't work with parameters.
MISSING: 1.proper Change CODE
*/
}
<!DOCTYPE html>
<html>
<head>
<meta charset = "UTF-8">
<title>Task1</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="*.js"></script>
</head>
<body>
<div class="inputbox">
Change From:
<input type="text" id="input1" value="banana"><br />
Change To:
<input type="text" id="input2" value="strawberries">
<button onclick="changeText()">Execute</button>
</div>
<p class="phrases">
This is a simple text. I want to <b>select a word or a phrase, and change it to another using two textboxes and a button to execute</b>. After execution, ALL of the matching words or phrases should be changed at one time. If it's possible, the "Change Text" function should only apply to WHOLE WORDS; When I select "Change from" - Banana to - strawberries only "Banana" strings should be changed instead of changing "Bananas" as well.
</p>
<p class="phrases">
TEST: Banana, apple, banana, apple, carrot, carrots, bananas, apples, banana, carrots.
</p>
</body>
</html>