2

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);">

1
  • Just delete the row for the find input and call the function using these parameters: findMyText('xxxxxx', document.getElementById('replacement').value); Commented Oct 28, 2017 at 13:11

2 Answers 2

2

Just delete the row for the find input and call the function using these parameters: findMyText('xxxxxx', document.getElementById('replacement').value);

This is what I mean:

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>Replacment</td><td><input id="replacement" name="replacement" type="text"></td></tr>
</table>
<input type="button" value="Update" onClick="findMyText('xxxxxx', document.getElementById('replacement').value);">

Sign up to request clarification or add additional context in comments.

Comments

1

You can just modify your onclick handler to have the fixed value xxxxx. Like this: onClick="findMyText('xxxxxx', document.getElementById('replacement').value);"

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('xxxxxx', document.getElementById('replacement').value);">

If you want the text to update "live", as the user types in, you can use onInput handler on the textbox.

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" onInput="findMyText('xxxxxx', document.getElementById('replacement').value);"></td>
  </tr>
</table>
<!--<input type="button" value="Update" onClick="findMyText('xxxxxx', document.getElementById('replacement').value);">-->

1 Comment

Thank you so much it's been getting at me for a while!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.