title: Traverse Nested Tables in JQuery
body: <p><strong>Need</strong>:
  I want to convert the data in my table into a comma separated value.</p>
  
  <p><strong>Problem</strong>:
  A table cell may have another table. (e.g. Row1, Cell 3 contains a table) The script needs to run for nested tables and capture the column only once. Here it is capturing "Col 3" twice.
  The number of nested tables is not known.
  It should be a generic script.</p>
  
  <p>Thank you.</p>
  
  <h2>my HTML file:</h2>
  
  <pre><code>&lt;html&gt;
   &lt;head&gt;
    &lt;script src="http://code.jquery.com/jquery-1.4.4.min.js" type="text/javascript"&gt;&lt;/script&gt; 
   &lt;script src="jquery.table2csv.0.1.1.min.js" type="text/javascript"&gt;&lt;/script&gt; 
   &lt;script type="text/javascript"&gt;
   jQuery(function() {
    jQuery("#tableone").table2csv({
     callback: function (csv) {
      //alert(csv);
     }
    });
   });
  &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
   &lt;table id="tableone" style="width:100%;"&gt;
    &lt;tbody&gt;
     &lt;tr&gt;
      &lt;td&gt;
       &lt;div &gt;
        Col 1
       &lt;/div&gt;
      &lt;/td&gt;
      &lt;td &gt;
       &lt;div &gt;
        Col 2
       &lt;/div&gt;
      &lt;/td&gt;
      &lt;td &gt;
       &lt;div &gt;
        &lt;table &gt;
         &lt;tbody&gt;
          &lt;tr&gt;
           &lt;td &gt;
            Col 3
           &lt;/td&gt;
          &lt;/tr&gt;
         &lt;/tbody&gt;
        &lt;/table&gt;
       &lt;/div&gt;
      &lt;/td&gt;
     &lt;/tr&gt;
     &lt;tr &gt;
      &lt;td &gt;
       &lt;div &gt;
        Data in Col 1
       &lt;/div&gt;
      &lt;/td&gt;
      &lt;td &gt;
       &lt;div &gt;
        Data in Col 2
       &lt;/div&gt;
      &lt;/td&gt;
      &lt;td &gt;
       &lt;div &gt;
        Data in Col 3
       &lt;/div&gt;
      &lt;/td&gt;
     &lt;/tr&gt;
    &lt;/tbody&gt;
   &lt;/table&gt;
   &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <hr>
  
  <h2>jquery.table2csv.0.1.1.min.js</h2>
  
  <pre><code>(function($)
   {$.fn.table2csv=function(options)
    {
     var defaults=
     {
      delimiter:",",callback:function(csv)
      {
       return csv;
      } 
     };
     var settings=$.extend(defaults,options);
     return this.each(function()
     {
      var name=$(this).find("caption").text();
      var csv="";
  
      $(this).find("td").each(function()
      {
       csv+="\""+$(this).text().replace(/(\")/gim,"\\\"\\\"")+"\""+",";
       alert(csv);
      })  
      csv=csv.replace(/\,$/gim,"");
      settings.callback(csv,name);
     });
  
    }
   })(jQuery);
  </code></pre>