954,600 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to add spacing between columns using JavaScript

This looks as it should except that it needs more spacing between the columns. How would I do that?

<script type="text/javascript">
/* <![CDATA[ */
var numRows = "10";
var numCols = "10";
if (isNaN(numRows) || isNaN(numCols)) {
}else {
    var tblHTML = "", rowHTML;
    for (var row = 0; row <= numRows; row++) {
        rowHTML = "";
        for (var col = 0; col <= numCols; col++) {
            if (row == 0) {
                rowHTML += "<td>" +
                    ((col == 0) ? " " : col) +
                    "</td>";
            } else if (col == 0) {
                rowHTML += "<td>" +
                    row +
                    "</td>";
            } else {
                rowHTML += "<td>" +
                    (row * col) +
                    "</td>";
            }
        }
        tblHTML += "<tr>" + rowHTML + "</tr>";
    }
    document.write("<table border=\"0\">" +
        tblHTML +
        "</table>");
}

/* ]]> */
</script>    
</head>
<body> 
</body>
</html>
astnrocker
Newbie Poster
9 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
 

AstnRocker

Easiest way is to control spacing in the HTML with table attributes, eg:

document.write("<table cellpadding=\"5\" cellspacing=\"2\" border=\"1\">" + tblHTML + "</table>");

Cellpadding and cellspacing add vertical as well as horizontal spacing.

Alternatively, you can use CSS, which gives more control, eg:

<style>
td {
	padding: 3px 12px;
}
</style>

This gives more horizontal padding than vertical (which cannot be achieved in HTML alone).Airshow

Airshow
WiFi Lounge Lizard
Moderator
2,683 posts since Apr 2009
Reputation Points: 321
Solved Threads: 372
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: