title: JavaScript: How to highlight text with pressing "Enter/Return"?
body: <p>I'm trying to create a functioning search box that highlights words in a text. However,   I cannot make it do the search by pressing Enter. Do you have any suggestions where is the flaw in the code? Thanks!</p>
  
  <p>HTML:</p>
  
  <pre><code>&lt;form&gt;
  &lt;input type="text" id="search" onkeypress="if (e.keycode==13) doSearch(document.getElementById('search').value)" placeholder="What are you looking for?"&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>JavaScript:</p>
  
  <pre><code>function doSearch(text)
  {
  if (window.find &amp;&amp; window.getSelection)
     {
  document.designMode = "on";
  var sel = window.getSelection();
  sel.collapse(document.body, 0);
  while (window.find(text))
        {
  document.getElementById("search").blur();
  document.execCommand("HiliteColor", false, "green");
  sel.collapseToEnd();
        }
  document.designMode = "off";
     }
  else if (document.body.createTextRange)
     {
  var textRange = document.body.createTextRange();
  while (textRange.findText(text))
        {
  textRange.execCommand("BackColor", false, "green");
  textRange.collapse(false);
        }
     }
  }
  </code></pre>