title: Set width of td depending on No of td using jquery
body: <p>I have several web pages with different amount of tables with different amount of columns.</p>
  
  <p>I was looking on the net for a jquery spinet which gets the number of columns of the table and depending on the number of columns will define the width of each column.</p>
  
  <p>Ex.</p>
  
  <pre><code>  if (noOfTdOnTable == 2) {
       tdWidth = "50%";
      }
      if (noOfTdOnTable == 3) {
        td1Width = "40%";
        td2Width = "40%";
        td3Width = "20%";
      }
      if (noOfTdOnTable == 4) {
        td1Width = "35%";
        td2Width = "25%";
        td3Width = "15%";
        td4Width = "15%";
      }
  </code></pre>
  
  <p><strong>Update</strong></p>
  
  <p>Using the only answer I was given I have this at the moment but only works when there is one table on the page and I could not figure out how to apply when there are two columns.</p>
  
  <pre><code>        var num = $("table &gt; td").length;
  
  
      if (num % 4 == 0) {
          $("table  &gt; td:eq(0)").css("width", "50%");
          $("table &gt; td:eq(1)").css("width", "30%");
          $("table &gt; td:eq(2)").css("width", "10%");
          $("table &gt; td:eq(3)").css("width", "10%");
      }
      if (num % 3 == 0) {
          $("table &gt; td:eq(0)").css("width", "50%");
          $("table &gt; td:eq(1)").css("width", "40%");
          $("table &gt; td:eq(2)").css("width", "10%");
      }
  </code></pre>
  
  <p>This is an example of the html, but the code will apply to lots of pages with different No of Tables but all tables will have either 2,3 or 4 columns.</p>
  
  <pre><code>&lt;html&gt;    
      &lt;table&gt;
             &lt;tr&gt;
                 &lt;td&gt;text&lt;/td&gt;
                 &lt;td&gt;text&lt;/td&gt;
                 &lt;td&gt;text&lt;/td&gt;
            &lt;/tr&gt;
      &lt;/table&gt;
  
      &lt;table&gt;
             &lt;tr&gt;
                 &lt;td&gt;text&lt;/td&gt;
                 &lt;td&gt;text&lt;/td&gt;
            &lt;/tr&gt;
      &lt;/table&gt;
  
      &lt;table&gt;
             &lt;tr&gt;
                 &lt;td&gt;text&lt;/td&gt;
                 &lt;td&gt;text&lt;/td&gt;
                 &lt;td&gt;text&lt;/td&gt;
                 &lt;td&gt;text&lt;/td&gt;
            &lt;/tr&gt;
      &lt;/table&gt;
  &lt;/html&gt;
  </code></pre>