Dynamically created tables appear outside the div tag.
I'm creating a responsive registration form. The input field takes the required number of rows for the table given by the user and creates the required number of rows with a fixed number of columns.
I created the table but it doesn't appear where I want it to. Please give me some advice. Also helps with the label issue in the first line, which is the title (th).
function createFamTable() { var rows = document.getElementById("family-rows").value; var tbl = document.createElement("table"); tbl.id = "family-table1"; var tblBody = document.createElement("tbody"); for (let i = 0; i < rows; i++) { var row = document.createElement("tr"); for (let j = 0; j < 5; j++) { var cell = document.createElement("td"); switch(j) { case j=0: var cellText = document.createElement('input'); cellText.placeholder = "Enter Name"; cell.appendChild(cellText); break; case j=1: var cellText = document.createElement('input'); cellText.placeholder = "Enter Relation"; cell.appendChild(cellText); break; case j=2: var cellText = document.createElement('input'); cellText.placeholder = "Enter Profession"; cell.appendChild(cellText); break; case j=3: var cellText = document.createElement('input'); cellText.placeholder = "Enter Age"; cell.appendChild(cellText); break; case j=4: var cellText = document.createElement('select'); var arr = ["Select Yes / No","Yes","No"]; for (var k=0;k<=2;k++){ var opt = document.createElement('option'); opt.value = arr[k]; opt.text = arr[k]; cellText.appendChild(opt); } cell.appendChild(cellText); break; } row.appendChild(cell); } tblBody.appendChild(row); } tbl.appendChild(tblBody); document.body.appendChild(tbl); cell.setAttribute("border", "2"); addFirstRow(); }
<div class="form third"> <div class="details education"> <span class="title">Family Details</span> <div class="fields"> <label>Number of Family Members<span style="color:red;font-size: smaller;">*</span></label> <input type="number" id="family-rows" placeholder="Example: 5" required> </div> <button class="createFamilyTable" onclick="createFamTable()">Add Details</button> <div class="buttons"> <div class="backBtn1"> <i class="uil uil-navigator"></i> <span class="btnText">Back</span> </div> <button class="nextBtn2"> <span class="btnText">Next</span> <i class="uil uil-navigator"></i> </button> </div> </div> </div> </form>
I want the table to appear inside the div tag (in that empty space).
I also tried adding a label title to the table. Please also help to resolve this issue.
P粉6449810292023-09-14 10:17:24
You did a great job; just some miner's mistake. Add an attribute (type="button") for the "Add Details" button; by default, the button type in the form is submit. Add the attribute as follows:
<button type="button" class="createFamilyTable" onclick="createFamTable()"> Add Details</button>
and add a div
anywhere<div id="form-table"></div>
and this
document.body.appendChild(tbl);Replace
with
document.getElementById('form-table').appendChild(tbl);
or
document.getElementById('form-table').innerHTML = tbl;