function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Avinash BarolaAvinash Barola 

I am not able to set column width of table

<apex:page standardController="Account">
    <script src="/soap/ajax/11.0/connection.js"></script>
    <script src="/soap/ajax/11.0/apex.js"></script>
    <script src="{!$Resource.jQuery}"/>
    <script type="text/javascript">
        
        queryResult= null; 
    var flag=false;
   var table ="<table id='tab' border =' 1 solid black '></table>";    
    window.onload=function(){  
        sforce.connection.sessionId = "{!$Api.Session_ID}";
        queryResult= sforce.connection.query("select Id,Name,Owner.name,Phone from Account order by isActive__C desc,name"); 
        document.write(table);
        accountDetail(); 
    };
    function accountDetail()
    {   
        document.getElementById('tab').innerHTML = "";
        document.getElementById("tab").style.columnWidth = "100px";
         var str = "<tr><th>ID</th><th><a href ='#' onclick = 'accountDetail()'>Name</th><th>owner Name</th><th>Phone</th></tr>";
        try {   
            var count = 0;
            if(queryResult != null && queryResult.size >0){ 
                var crecords = queryResult.getArray("records");
                if(!flag)
                  {  flag=true;
                     for (var i=0; i<crecords.length; i++) { 
                         str = str + "<tr><td  width = '50px' style = 'text-overflow:ellipsis'>"+crecords[i].Id+"</td><td>"+crecords[i].Name+"</td><td>"+crecords[i].Owner.Name+"</td><td>"+crecords[i].Phone+"</td></tr>";
                    }
                }
             
                else
                { flag=false;
                    for (var i=crecords.length-1;i>=0; i--) { 
                         str = str + '<tr><td>'+crecords[i].Id+'</td><td>'+crecords[i].Name+'</td><td>'+crecords[i].Owner.Name+'</td><td>'+crecords[i].Phone+'</td></tr>';
                    }
                }
            }
           document.getElementById('tab').innerHTML=str;

        }
        catch(error){
            alert(error);
        }
    }
    
    </script>
</apex:page>
Sumit Kumar Singh 9Sumit Kumar Singh 9
Hello Avinash Barola,

Try to inspect the Table ID by pressing F12 OR Right click on the table and choose "Inspect Element".
I am suspecting that the ID of the table would be different.


Thanks,
Sumit Kumar Singh
LakshmanLakshman
Try this, it worked for me:
<apex:page standardController="Account">
    <script src="/soap/ajax/11.0/connection.js"></script>
    <script src="/soap/ajax/11.0/apex.js"></script>
    <script src="{!$Resource.jQuery}"/>
    <script type="text/javascript">
        
        queryResult= null; 
    var flag=false;
   var table ="<table id='tab' border =' 1 solid black '></table>";    
    window.onload=function(){  
        sforce.connection.sessionId = "{!$Api.Session_ID}";
        queryResult= sforce.connection.query("select Id,Name,Owner.name,Phone from Account order by isActive__C desc,name"); 
        document.write(table);
        accountDetail(); 
    };
    function accountDetail()
    {   
        document.getElementById('tab').innerHTML = "";
        document.getElementById("tab").style.width = "100px";
         var str = "<tr><th>ID</th><th><a href ='#' onclick = 'accountDetail()'>Name</th><th>owner Name</th><th>Phone</th></tr>";
        try {   
            var count = 0;
            if(queryResult != null && queryResult.size >0){ 
                var crecords = queryResult.getArray("records");
                if(!flag)
                  {  flag=true;
                     for (var i=0; i<crecords.length; i++) { 
                         str = str + "<tr><td  width = '50px' style = 'text-overflow:ellipsis'>"+crecords[i].Id+"</td><td>"+crecords[i].Name+"</td><td>"+crecords[i].Owner.Name+"</td><td>"+crecords[i].Phone+"</td></tr>";
                    }
                }
             
                else
                { flag=false;
                    for (var i=crecords.length-1;i>=0; i--) { 
                         str = str + '<tr><td>'+crecords[i].Id+'</td><td>'+crecords[i].Name+'</td><td>'+crecords[i].Owner.Name+'</td><td>'+crecords[i].Phone+'</td></tr>';
                    }
                }
            }
           document.getElementById('tab').innerHTML=str;

        }
        catch(error){
            alert(error);
        }
    }
    
    </script>
</apex:page>

Please mark it as best answer so that it helps others. 
LakshmanLakshman
If you want to set a fixed width to each column then you can use colgroup- http://htmlhelp.com/reference/html40/tables/colgroup.html 
or col http://www.w3schools.com/tags/att_col_width.asp 

Let us know if it helps.