Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Input in HTML Table

function toggleInputCells(button) {
  var cells = document.getElementsByClassName('input');
  
  for (var i = 0; i < cells.length; i++) {
    var cell = cells[i];
    var input = cell.getElementsByTagName('input')[0];

    if (input != null) {
      var text = input.value;
      cell.innerHTML = text;
    } else {
      var text = cell.innerHTML;
      cell.innerHTML = '';
      var input = document.createElement('input');
      input.type = "text";
      input.value = text;
      cell.appendChild(input);
    }
  }
}


var rows = 10;
var cols = 4;

var table = document.createElement('table');
table.classList.add('table');

var thead = document.createElement('thead');
var headRow = document.createElement('tr');
var columnNames = ["Col1", "Col2", "Col3", "Col4"];

for (var i = 0; i < 4; i++) {
  var th = document.createElement('th');
  th.appendChild(document.createTextNode(columnNames[i]));
  headRow.appendChild(th);
}

thead.appendChild(headRow);

var tbody = document.createElement('tbody');

for (var i = 0; i < rows; i++) {
  var tr = document.createElement('tr');

  for (var j = 0; j < cols; j++) {
    var td = document.createElement('td');

    if (j == 3) {
      td.classList.add("input");
      var input = document.createElement('input');
      input.type = "text";
      input.value = 'Row #' + (i + 1); // Add a value?
      td.appendChild(input);
      tr.appendChild(td);
      continue;
    }
    td.appendChild(document.createTextNode("x"));
    tr.appendChild(td);
  }

  tbody.appendChild(tr);
}

table.appendChild(thead);
table.appendChild(tbody);
document.body.appendChild(table);
Comment

PREVIOUS NEXT
Code Example
Javascript :: json with postgresql 
Javascript :: how to create instance of class in javascript 
Javascript :: get difference of minutes between two time based on am, pm 
Javascript :: play audio file in phaser 
Javascript :: react set state before render 
Javascript :: node js middleware for parsing formdata 
Javascript :: js set value in object only if defined 
Javascript :: hide playback speed from videojs 
Javascript :: test cases in react 
Javascript :: console log javascript 
Javascript :: reactjs npm take photo 
Javascript :: javascript typeof array 
Javascript :: how to disable eval in javascript 
Javascript :: nested template strings js 
Javascript :: vscode angular: running ngcc 
Javascript :: js display property 
Javascript :: how to change css using javascript 
Javascript :: chaine de caractère dans une autres js 
Javascript :: Expo Location get getCurrentPositionAsync not returning anything 
Javascript :: material icons angular 
Javascript :: react useeffect hook 
Javascript :: Sum of Polygon Angles in javascript 
Javascript :: empty array js 
Javascript :: socket io express 
Javascript :: Fake Binary 
Javascript :: add pdf in react app 
Javascript :: vue router "savedposition" with ajax call 
Javascript :: object declaration in javascript 
Javascript :: Printer Print using HTML 
Javascript :: javascript copy object except one property 
ADD CONTENT
Topic
Content
Source link
Name
2+3 =