Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Some points:</p> <p>Changing your regExp from <code>/^\s+/</code> to <code>/^\s+|\s+$/g</code> might be useful as it will trim trailing whitespace, too.</p> <p>Change <code>if (text_input === "") {</code> to <code>if (removeSpaces === "") {</code> otherwise you get horrible results if you just enter a space.</p> <p>Why are you declaring the functions <code>highlight</code>, <code>removeSpan</code> and <code>addSpan</code> when you only call them in one place? Just put their code where you call them (unless, of course, you want to use them elsewhere, too).</p> <p>Why are you creating the temporary vars <code>newstr</code> and <code>newstr2</code>? Just reassign the result of the <code>anchor_text.replace()</code> call back into <code>anchor_text</code>.</p> <p>Pass regexes in the replace function instead of strings.</p> <p>Implementing all of these changes, we get:</p> <pre><code>document.getElementById('check-list').onclick = function() { text_input = document.search.search_list.value.replace(/^\s+|\s+$/g, ""); if (text_input.length) { var anchor = document.getElementById('results').getElementsByTagName('a'); var alength = anchor.length; for (var x = 0; x &lt; alength; x++) { var anchor_text = anchor[x].innerHTML; anchor_text = anchor_text .replace(/&lt;span class="highlight"&gt;/gi, "") .replace(/&lt;\/span&gt;/gi, ""); anchor[x].innerHTML = anchor_text; var re = new RegExp(text_input, "gi"); if (anchor_text.search(re) !== -1) { anchor_text = anchor_text.replace(re, "&lt;span class='highlight'&gt;$&amp;&lt;/span&gt;"); anchor[x].innerHTML = anchor_text; } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload