So I have a javascript find and replace code here that finds what you search and replaces with an input. I need the code to find "xxxxxx" without asking for an input so the only input left would be the replace box which would automatically change xxxxxx to the user input. Any suggestions? here's my code:
var haystackText = "";
function findMyText(needle, replacement) {
if (haystackText.length == 0) {
haystackText = document.getElementById("haystack").innerHTML;
}
var match = new RegExp(needle, "ig");
var replaced = "";
if (replacement.length > 0) {
replaced = haystackText.replace(match, replacement);
} else {
var boldText = "<div style=\"background-color: yellow; display: inline; font-weight: bold;\">" + needle + "</div>";
replaced = haystackText.replace(match, boldText);
}
document.getElementById("haystack").innerHTML = replaced;
}
<div id="haystack">
<p>Paragraph paragraph paragraph xxxxxx paragraph</p>
<p>Paragraph paragraph paragraph xxxxxx paragraph</p>
<p>Paragraph paragraph paragraph xxxxxx paragraph</p>
</div>
<br>
<table>
<tr>
<td>Find</td>
<td><input id="needle" name="needle" type="text"></td>
</tr>
<tr>
<td>Replacment</td>
<td><input id="replacement" name="replacement" type="text"></td>
</tr>
</table>
<input type="button" value="Update" onClick="findMyText(document.getElementById('needle').value, document.getElementById('replacement').value);">
findMyText('xxxxxx', document.getElementById('replacement').value);