Your code works for the input
(not for the select
), but I would advise an overall clearer, less error-prone, different approach. It creates nodes the proper way (using document.createElement()
) instead of setting innerHTML
- avoiding escaping errors, invalid HTML tags and other common mistakes
.
var count = 2;
function addRow() {
var table = document.getElementById("studenttable");
var row = table.insertRow(-1);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var ipt = document.createElement('input');
ipt.setAttribute("type", "text");
ipt.name = "nametext" + count; // setting unique NAME
ipt.id = "nametext" + count; // setting unique ID
ipt.setAttribute("required", "true");
cell1.appendChild(ipt);
var theDiv = document.createElement('div');
theDiv.setAttribute("class", "dropdown dropdown-dark");
var theSelect = document.createElement('select');
theSelect.name = "select"+count; // setting unique NAME
theSelect.id = "select"+count; // setting unique ID
theSelect.setAttribute("class", "dropdown-select");
// option "Select an option"
var opt0 = document.createElement('option');
opt0.value = "";
opt0.text = "Select an option";
theSelect.appendChild(opt0);
// option "Male"
var opt1 = document.createElement('option');
opt1.value = "1";
opt1.text = "Male";
theSelect.appendChild(opt1);
// option "Female"
var opt2 = document.createElement('option');
opt2.value = "2";
opt2.text = "Female";
theSelect.appendChild(opt2);
theDiv.appendChild(theSelect);
cell2.appendChild(theDiv);
count++;
}