Skip to content

Instantly share code, notes, and snippets.

@Jaskaranbir
Created May 17, 2016 00:26
Show Gist options
  • Save Jaskaranbir/b036610445e033fb9823bb78228a8ce5 to your computer and use it in GitHub Desktop.
Save Jaskaranbir/b036610445e033fb9823bb78228a8ce5 to your computer and use it in GitHub Desktop.
To generate tabular grid in html using javascript
var body = window.document.getElementsByTagName('body')[0];
// Square Grid
(function genGrid(size) {
var table = document.createElement('table');
table.setAttribute('border', '2');
table.setAttribute('cellspacing', '0');
table.setAttribute('cellpadding', '40');
// This variable isn't required.
// Just exists to make this script code friendly so the grid size can be changed
// without modifying more than one word.
var temp = (size + 1);
size *= size;
// Generate the grid
while (--size >= -1) {
var row = document.createElement('tr');
while (size % temp != 0) {
row.appendChild(document.createElement('td'));
--size;
}
table.appendChild(row);
}
body.appendChild(table);
})(10); // This parameter is the size of grid
// Rectangular Grid
(function genGrid(width, height) {
var table = document.createElement('table');
table.setAttribute('border', '2');
table.setAttribute('cellspacing', '0');
table.setAttribute('cellpadding', '10');
// Generate the grid
while (width-- > 0) {
var tempHeight = height;
var row = document.createElement('tr');
while (tempHeight-- > 0)
row.appendChild(document.createElement('td'));
table.appendChild(row);
}
body.appendChild(table);
})(25, 27); // This parameter is the size of grid
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment