Skip to content

Instantly share code, notes, and snippets.

Created December 10, 2016 00:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/e54737801af6411d1dd8f442d277084d to your computer and use it in GitHub Desktop.
Save anonymous/e54737801af6411d1dd8f442d277084d to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Prova di griglia in input</title>
<script>
window.onload = function() {
var num_rows = 0;
var ult_num = 5
function setInputAttributes(i, num) {
if (num === ult_num)
return false;
i.setAttribute('type', 'text');
i.setAttribute('name', "input_" + String(num_rows) + "_" + String(num));
if (num === (ult_num - 1))
i.setAttribute('class', "ult_input");
return true;
}
function insertNewLines() {
var g = document.querySelector("#main_grid");
var r = document.createElement("tr");
r.setAttribute("class", "row");
g.appendChild(r);
num_rows++;
for(var i = 0; ; i++) {
var col = document.createElement('td');
var inp = document.createElement('input');
col.appendChild(inp);
if (setInputAttributes(inp, i) === false)
break; // esce se ha raggiunt il numero massimo di input
r.appendChild(col);
}
var ultCellRows = document.querySelectorAll(".ult_input");
var ultCell;
for (var i = 0; i < num_rows; i++) {
if (i === num_rows - 1) {
ultCell = ultCellRows[i];
} else {
ultCellRows[i].removeEventListener('keypress', keypressUltCell, false);
}
}
if (document.addEventListener) {
ultCell.addEventListener('keypress', keypressUltCell, false);
}
}
function keypressUltCell(event) {
var e = event || window.event;
var code = e.charCode || e.keyCode;
if (e.key === "Tab")
insertNewLines();
}
insertNewLines();
}
</script>
</head>
<body>
<table id="main_grid">
</table>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment