<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Random Snippets &#187; find</title>
	<atom:link href="http://www.randomsnippets.com/tag/find/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.randomsnippets.com</link>
	<description>Random Snippets of Code and Ideas for any Web Developer</description>
	<lastBuildDate>Wed, 05 Aug 2009 14:32:02 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to find and replace text dynamically via JavaScript</title>
		<link>http://www.randomsnippets.com/2008/03/07/how-to-find-and-replace-text-dynamically-via-javascript/</link>
		<comments>http://www.randomsnippets.com/2008/03/07/how-to-find-and-replace-text-dynamically-via-javascript/#comments</comments>
		<pubDate>Fri, 07 Mar 2008 07:18:20 +0000</pubDate>
		<dc:creator>Knix</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[dynamic]]></category>
		<category><![CDATA[find]]></category>
		<category><![CDATA[find and replace]]></category>
		<category><![CDATA[findMyText]]></category>
		<category><![CDATA[haystack]]></category>
		<category><![CDATA[haystackText]]></category>
		<category><![CDATA[javascript function]]></category>
		<category><![CDATA[onclick event]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[replace]]></category>
		<category><![CDATA[Replacement]]></category>
		<category><![CDATA[replacement text]]></category>
		<category><![CDATA[text replacement]]></category>

		<guid isPermaLink="false">http://www.randomsnippets.com/2008/03/07/how-to-find-and-replace-text-dynamically-via-javascript/</guid>
		<description><![CDATA[
This is a neat little script that demonstrates two things:
1) Find &#8211; If only the Find box is filled in, then the findMyText() JavaScript function will just perform a find for the div that is given.  If there is a match, the text will be highlighted and marked in bold.
2) Find and Replace &#8211; [...]]]></description>
			<content:encoded><![CDATA[<p><script src="/wp-includes/js/findAndReplace.js" language="Javascript" type="text/javascript"></script><br />
This is a neat little script that demonstrates two things:</p>
<p>1) <b>Find</b> &#8211; If only the <b>Find</b> box is filled in, then the <b>findMyText()</b> JavaScript function will just perform a find for the div that is given.  If there is a match, the text will be highlighted and marked in bold.</p>
<p>2) <b>Find and Replace</b> &#8211; If both the <b>Find</b> and <b>Replace</b> boxes are filled in, then the <b>findMyText()</b> JavaScript function will do just that. </p>
<fieldset>
<legend>Demo</legend>
<div id="haystack">
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
<p></p>
<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="Find" onClick="findMyText(document.getElementById('needle').value, document.getElementById('replacement').value);">
</fieldset>
<p><span id="more-11"></span><br />
Here is the plain HTML:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;div id=&quot;haystack&quot;&gt;
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
&lt;/div&gt;
&lt;br&gt;
&lt;table&gt;
&lt;tr&gt;&lt;td&gt;Find&lt;/td&gt;&lt;td&gt;&lt;input id=&quot;needle&quot; name=&quot;needle&quot; type=&quot;text&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Replacment&lt;/td&gt;&lt;td&gt;&lt;input id=&quot;replacement&quot; name=&quot;replacement&quot; type=&quot;text&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;
&lt;input type=&quot;button&quot; value=&quot;Find&quot; onClick=&quot;findMyText(document.getElementById('needle').value, document.getElementById('replacement').value);&quot;&gt;</pre></td></tr></table></div>

<p>
Please excuse the use of the table for the text inputs and labels.  WordPress does some automatic formatting which is annoying sometimes but it is still a great application =)  The only thing of importance here is on line 9 where the <b>onClick</b> event executes the <b>findMyText()</b> function and passes 2 arguments:</p>
<ul>
<li><b>Needle</b> &#8211; This is the text that we are trying to find in the haystack of text.</li>
<li><b>Replacement</b> &#8211; This is our replacement text for any needle that we find.</li>
</ul>
<p>Here is our JavaScript function:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> haystackText <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&quot;</span>;
<span style="color: #003366; font-weight: bold;">function</span> findMyText<span style="color: #009900;">&#40;</span>needle<span style="color: #339933;">,</span> replacement<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>haystackText.<span style="color: #660066;">length</span> <span style="color: #339933;">==</span> 0<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          haystackText <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;haystack&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">innerHTML</span>;
     <span style="color: #009900;">&#125;</span>
     <span style="color: #003366; font-weight: bold;">var</span> match <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> RegExp<span style="color: #009900;">&#40;</span>needle<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;ig&quot;</span><span style="color: #009900;">&#41;</span>;     
     <span style="color: #003366; font-weight: bold;">var</span> replaced <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&quot;</span>;
     <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>replacement.<span style="color: #660066;">length</span> <span style="color: #339933;">&gt;</span> 0<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          replaced <span style="color: #339933;">=</span> haystackText.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span>match<span style="color: #339933;">,</span> replacement<span style="color: #009900;">&#41;</span>;
     <span style="color: #009900;">&#125;</span>
     <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #003366; font-weight: bold;">var</span> boldText <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&lt;div style=<span style="color: #000099; font-weight: bold;">\&quot;</span>background-color: yellow; display: inline; font-weight: bold;<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&quot;</span> <span style="color: #339933;">+</span> needle <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;&lt;/div&gt;&quot;</span>;
          replaced <span style="color: #339933;">=</span> haystackText.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span>match<span style="color: #339933;">,</span> boldText<span style="color: #009900;">&#41;</span>;
     <span style="color: #009900;">&#125;</span>
     document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;haystack&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> replaced;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Like many of the scripts that you see here, using the JavaScript DOM objects is the key to doing the find and replace dynamically for our website.  </p>
<p>The script asks for two parameters: the needle which we are looking for in our text and the text we want to replace it with.  Please note that the replacement text is totally <i>optional</i>.  If no replacement text is given, then the behavior of the function changes a bit.</p>
<p>The <b>haystackText</b> variable is outside of the function in line 1 because we want to store the original text and always base our searches on the original text.  In lines 3-5 is where we store the original text in our <b>haystack</b> div if it is not already set.</p>
<p>Line 6 creates a regular expression object where we place our <b>needle</b> variable along with two flags:</p>
<ul>
<li><b>i</b> &#8211; This flag causes the regular expression match to be case-insensitive.</li>
<li><b>g</b> &#8211; This flag causes the regular expression match to be global so it matches every occurrence instead of just the first one.</li>
</ul>
<p>Lines 8-10 checks to see if the <b>replacement</b> text was set.  If so, we will use the <b>replace</b> method on our <b>haystackText</b> string object and feed it our <b>match</b> and <b>replacement</b> texts.  The resulting string will be saved in the <b>replaced</b> variable.</p>
<p>If the <b>replacement</b> text was not set, the <b>else</b> block in lines 11-14 will be executed.  We first prepare a variable of our <b>needle</b> that has a bit of extra styling such as a yellow background and bold text so it stands out better.  Next, we just use the <b>replace</b> method on our <b>haystackText</b> string object and pass the <b>match</b> and <b>replacement</b> texts.  The resulting string will be saved in the <b>replaced</b> variable.  </p>
<p>Lastly, the <b>replaced</b> variable which contains our results is used to substitute what was originally in our <b>haystack</b> div.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.randomsnippets.com/2008/03/07/how-to-find-and-replace-text-dynamically-via-javascript/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
