Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3303760
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:59:27+00:00 2026-05-17T20:59:27+00:00

I have several web pages with different amount of tables with different amount of

  • 0

I have several web pages with different amount of tables with different amount of columns.

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.

Ex.

  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%";
    }

Update

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.

        var num = $("table > td").length;


    if (num % 4 == 0) {
        $("table  > td:eq(0)").css("width", "50%");
        $("table > td:eq(1)").css("width", "30%");
        $("table > td:eq(2)").css("width", "10%");
        $("table > td:eq(3)").css("width", "10%");
    }
    if (num % 3 == 0) {
        $("table > td:eq(0)").css("width", "50%");
        $("table > td:eq(1)").css("width", "40%");
        $("table > td:eq(2)").css("width", "10%");
    }

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.

<html>    
    <table>
           <tr>
               <td>text</td>
               <td>text</td>
               <td>text</td>
          </tr>
    </table>

    <table>
           <tr>
               <td>text</td>
               <td>text</td>
          </tr>
    </table>

    <table>
           <tr>
               <td>text</td>
               <td>text</td>
               <td>text</td>
               <td>text</td>
          </tr>
    </table>
</html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T20:59:27+00:00Added an answer on May 17, 2026 at 8:59 pm

    To get the number of columns:

    var num = $("#table > tr > td").length;
    

    To specify the width:

    $("#table > tr > td").width(w + "px");
    

    I hope this was what you were looking for

    Edit:

    To specify the width to a specific column:

    //if you’ve specified an id to each td

    $("#td1").width(td1Width+"px");
    

    //if you just use classes to identify them

    $("td.td1", "#table1").width(td1Width+"px");
    

    I would also recommend you to look into find() and end() to select columns in an efficient way. since making a $() call is a more expensive operation:

    $("#table1").find("#td1").width(td1Width+"px").end().find("#td2")...
    

    Edit 2

    try this instead

    $("table > tr > td:eq(0)").css("width", "50%");
    

    or even better

    $("table > tr > td").eq(0).css("width", "50%").end()
                        .eq(1).css(...etc;
    

    “table” will select all tables on the page; use id or class to identify which table you require.

    Edit 3 (final!!)

    Ok, now i can see all the code i can give a better answer. try this:

    var num;
    var $tds;
    $("table").each(function(i, t) {
       $tds = $("td", t);
       num = $tds.length;
    
       if (num % 4 == 0) {
            $tds.eq(0).css("width", "50%").end()
                .eq(1).css("width", "30%").end()
                .eq(2).css("width", "10%").end()
                .eq(3).css("width", "10%");
        }
        if (num % 3 == 0) {
            //etc
        }
    });
    

    I hope this is a better answer 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several web pages containing tables, for which I'd like to have line-borders
I have a web page with several jQuery ajax calls which fire asynchronously at
I have a web application (ASP.NET MVC3) which uses the jquery ui tab control
I have several web pages on several different sites that I want to mirror
We have build several web service based on .net. Now we want to create
I have several DropDown controls on a Web Form which are filled using AJAX
On my web page, I'd like to have a group of several concentric circles
I have several web applications in production that utilize NFS mounts to share resources
I have several web service written formerley and deployed on tomcat. Now I am
I have several web applications that make use of packages using WebActivator. On my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.