Created
December 10, 2016 00:27
-
-
Save anonymous/e54737801af6411d1dd8f442d277084d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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