javascript - How to add Dynamically table row including text boxes and select box with Different Ids
Get the solution ↓↓↓Solution:
But I am unable to add a select box when adding a new row. How can I do this?
The select list can be cloned using Node.cloneNode(). And the original select list can be located with a selector using Element.querySelector(), like in the code below:
var selectList = table.rows[2].querySelector('select');
var element2 = selectList.cloneNode(true); //pass true for deep clone (to include options)
//set the id property
element2.id = 'selected-item'+rowCount;
cell2.appendChild(element2);
See a demonstration of this in the snippet below.
As others have mentioned, jQuery's .clone() method works similarly. Since jQuery was tagged in the original post, this could be used as well, using .attr() to set the id attribute and .appendTo() to add the select list to a cell in the new table row:
var selectList = $('select.form-control');
var element2 = selectList.clone(); //performs deep clone by default
element2.attr('id','selected-item'+rowCount);
element2.appendTo(cell2);
function addRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var cell1 = row.insertCell(0);
var element1 = document.createElement("input");
element1.type = "checkbox";
element1.id = 'text' + rowCount + '';
element1.name = "chkbox[]";
cell1.appendChild(element1);
//var cell2 = row.insertCell(1);
//cell2.innerHTML = rowCount + 1;
var cell2 = row.insertCell(1);
var selectList = table.rows[2].querySelector('select');
/*var element2 = document.createElement("select");
element2.className = "form-control";
element2.id = 'item-name' + rowCount + '';
element2.name = "selected_item[]";
var existingOptions = table.rows[0].querySelector('option');
existingOptions.each(function(option) {
element2.appendchild(option.cloneNode(true));
});*/
var element2 = selectList.cloneNode(true);
element2.id = 'selected-item'+rowCount;
cell2.appendChild(element2);
var cell3 = row.insertCell(2);
var element3 = document.createElement("input");
element3.type = "text";
element3.className = "form-control";
element3.title = "Item-name";
element3.id = 'qty' + rowCount + '';
element3.name = "qty[]";
cell3.appendChild(element3);
var cell4 = row.insertCell(3);
var element4 = document.createElement("input");
element4.type = "text";
element4.className = "form-control";
element4.title = "Item-name";
element4.id = 'unit-price-' + rowCount + '';
element4.name = "unit-price[]";
cell4.appendChild(element4);
var cell5 = row.insertCell(4);
var element5 = document.createElement("input");
element5.type = "text";
element5.className = "form-control";
element5.title = "Item-name";
element5.id = 'toatal-' + rowCount + '';
element5.name = "total-[]";
cell5.appendChild(element5);
}
function deleteRow(tableID) {
try {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
for (var i = 0; i < rowCount; i++) {
var row = table.rows[i];
var chkbox = row.cells[0].childNodes[0];
if (null != chkbox && true == chkbox.checked) {
if (rowCount <= 3) {
alert("Cannot delete all the rows.");
break;
}
table.deleteRow(i);
rowCount--;
i--;
}
}
} catch (e) {
alert(e);
}
}
<table class="table table-striped table-hover" style="background-color:white; text-align: center;" id="item-table">
<tr>
<td></td>
<td> </td>
<td></td>
<td><input type="button" value="Add Row" id="btn-addrow" onclick="addRow('item-table');" class="btn-success form-control" /></td>
<td><input type="button" value="Remove" onclick="deleteRow('item-table');" class="form-control btn-danger" /></td>
</tr>
<tr>
<th></th>
<th style="text-align: center">Item Name</th>
<th style="text-align: center">Quantity</th>
<th style="text-align: center">unit Price</th>
<th style="text-align: center">total</th>
</tr>
<tr>
<td><input type="checkbox"></td>
<td class="col-md-4">
<select name="selected_item[]" class="form-control">
<option></option>
<option value="laurie">Laurie</option>
<option value="sam">Sam</option>
</select>
</td>
<td><input type="text" name="it_re_qty[]" class="form-control"></td>
<td><input type="text" id="y" name="unit_price[]" class="form-control"></td>
<td><input type="text" id="it_re_qty" name="total[]" class="form-control col-md-1"></td>
<!--<td> <input type="button" value="Add Row" id="btn-addrow" onclick="addRow('item-table');idincrement;" class="btn-success form-control"/></td>
<td><input type="button" value="Remove" onclick="deleteRow('item-table');" class="form-control btn-danger" /></td>
-->
</tr>
</table>
Answer
Solution:
Not sure exactly what you mean, but if you're talking about the 'Item Name' Column, try changing:
var element2 = document.createElement("input");
to
var element2 = document.createElement("select");
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: a non well formed numeric value encountered
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
About the technologies asked in this question
JavaScript
JavaScript is a multi-paradigm language that supports event-driven, functional, and mandatory (including object-oriented and prototype-based) programming types. Originally JavaScript was only used on the client side. JavaScript is now still used as a server-side programming language. To summarize, we can say that JavaScript is the language of the Internet.
https://www.javascript.com/
JQuery
JQuery is arguably the most popular JavaScript library with so many features for modern development. JQuery is a fast and concise JavaScript library created by John Resig in 2006. It is a cross-platform JavaScript library designed to simplify client-side HTML scripting. Over 19 million websites are currently using jQuery! Companies like WordPress, Facebook, Google, IBM and many more rely on jQuery to provide a kind of web browsing experience.
https://jquery.com/
HTML
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to programmierfrage.com
programmierfrage.com is a question and answer site for professional web developers, programming enthusiasts and website builders. Site created and operated by the community. Together with you, we create a free library of detailed answers to any question on programming, web development, website creation and website administration.
Get answers to specific questions
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Help Others Solve Their Issues
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.