<?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>Welcome to my world. &#187; JavaScript</title>
	<atom:link href="http://www.marceble.com/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.marceble.com</link>
	<description>Marc E. Eble</description>
	<lastBuildDate>Thu, 22 Apr 2010 20:47:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
		<item>
		<title>Simple JQuery Table Row Filter</title>
		<link>http://www.marceble.com/2010/02/simple-jquery-table-row-filter/</link>
		<comments>http://www.marceble.com/2010/02/simple-jquery-table-row-filter/#comments</comments>
		<pubDate>Thu, 11 Feb 2010 08:08:55 +0000</pubDate>
		<dc:creator>Marc</dc:creator>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[JQuery]]></category>

		<guid isPermaLink="false">http://www.marceble.com/?p=220</guid>
		<description><![CDATA[Here is a quick piece of JQuery JavaScript code I had to write today to filter the rows shown in a table. Search terms are seperated by a space. The code should be self explanatory. Comments welcome. Click here to see the code below in action. &#60;html&#62; &#60;head&#62; &#60;script src="jquery-1.4.1.min.js"&#62;&#60;/script&#62; &#60;script&#62;  $(document).ready(function() {       $("#searchInput").keyup(function(){  //hide all the [...]]]></description>
			<content:encoded><![CDATA[<p id="top" />Here is a quick piece of JQuery JavaScript code I had to write today to filter the rows shown in a table. Search terms are seperated by a space. The code should be self explanatory. Comments welcome.</p>
<p><a  class="thickbox" href="http://marceble.com/2010/jqueryfilter/index.html?TB_iframe=true&#038;width=720&#038;height=540" target="_blank">Click here to see the code below in action.</a></p>
<pre><strong>&lt;html&gt;
&lt;head&gt;
&lt;script src="jquery-1.4.1.min.js"&gt;&lt;/script&gt;
&lt;script&gt;
 $(document).ready(function() {
      $("#searchInput").keyup(function(){
</strong> //hide all the rows<strong>
          $("#fbody").find("tr").hide();
</strong> //split the current value of searchInput<strong>
          var data = this.value.split(" ");
</strong> //create a jquery object of the rows<strong>
          var jo = $("#fbody").find("tr");
</strong> //Recursively filter the jquery object to get results. <strong>
          $.each(data, function(i, v){
              jo = jo.filter("*:contains('"+v+"')");
          });
</strong> //show the rows that match.<strong>
          jo.show();
</strong> //Removes the placeholder text <strong>
      }).focus(function(){
          this.value="";
          $(this).css({"color":"black"});
          $(this).unbind('focus');
      }).css({"color":"#C0C0C0"});
  });
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
 &lt;input id="searchInput" value="Type To Filter"&gt;&lt;br/&gt;
 &lt;table&gt;
  &lt;thead&gt;
    </strong><strong>&lt;tr&gt;&lt;th&gt;Column1&lt;/th&gt;&lt;th&gt;Column2&lt;/th&gt;&lt;/tr&gt;
</strong><strong>  &lt;/thead&gt;
  &lt;tbody id="fbody"&gt;
    &lt;tr&gt;&lt;td&gt;cat&lt;/td&gt;&lt;td&gt;one&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;dog&lt;/td&gt;&lt;td&gt;two&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;cat&lt;/td&gt;&lt;td&gt;three&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;moose&lt;/td&gt;&lt;td&gt;four&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;mouse&lt;/td&gt;&lt;td&gt;five&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;dog&lt;/td&gt;&lt;td&gt;six&lt;/td&gt;&lt;/tr&gt;
  &lt;/tbody&gt;
 &lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;</strong></pre>
<h2>UPDATE:</h2>
<p>To make this be insensitive to case, you need to modify it slightly.<br />
The following code creates a custom jquery selector for case-insensitive matching:</p>
<pre><strong>$.expr[':'].containsIgnoreCase = function(e,i,m){
    </strong><strong>return jQuery(e).text().toUpperCase().indexOf(m[3].toUpperCase())&gt;=0;
};
</strong>
The complete code would then be:
<pre>
<strong>&lt;html&gt;
&lt;head&gt;
&lt;script src="jquery-1.4.1.min.js"&gt;&lt;/script&gt;
&lt;script&gt;
 $(document).ready(function() {
 </strong>//Declare the custom selector 'containsIgnoreCase'.<strong>
      $.expr[':'].containsIgnoreCase = function(n,i,m){
          </strong><strong>return jQuery(n).text().toUpperCase().indexOf(m[3].toUpperCase())&gt;=0;
      };
  
      $("#searchInput").keyup(function(){

          $("#fbody").find("tr").hide();
          var data = this.value.split(" ");
          var jo = $("#fbody").find("tr");
          $.each(data, function(i, v){

               </strong>//Use the new containsIgnoreCase function instead
<strong>               jo = jo.filter("*:containsIgnoreCase('"+v+"')");
          });
</strong><strong>
          jo.show();
</strong><strong>
      }).focus(function(){
          this.value="";
          $(this).css({"color":"black"});
          $(this).unbind('focus');
      }).css({"color":"#C0C0C0"});
  });
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;input value="Type To Filter"&gt;&lt;br/&gt;
&lt;table&gt;
  &lt;thead&gt;
    </strong><strong>&lt;tr&gt;&lt;th&gt;Column1&lt;/th&gt;&lt;th&gt;Column2&lt;/th&gt;&lt;/tr&gt;
</strong><strong>  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;&lt;td&gt;cat&lt;/td&gt;&lt;td&gt;one&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;dog&lt;/td&gt;&lt;td&gt;two&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;cat&lt;/td&gt;&lt;td&gt;three&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;moose&lt;/td&gt;&lt;td&gt;four&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;mouse&lt;/td&gt;&lt;td&gt;five&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;dog&lt;/td&gt;&lt;td&gt;six&lt;/td&gt;&lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;</strong></pre>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.marceble.com/2010/02/simple-jquery-table-row-filter/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

