I want to iterate through my dynamically created table and create an array of elements Project Name, Client Name, and Field Rate at each index to populate and pass through a JSON object
Here is how my table looks:
<table id="project-table" class="table table-striped table-hover">
<thead>
<tr>
<th>Project Name</th>
<th>Client Name</th>
<th>Field Rate</th>
<th>Delete Row</th>
</tr>
</thead>
<tbody>
<%--jquery will append our data here... --%>
</tbody>
</table>
<button type="button" onclick="projectTable()" class="btn btn-primary">Add Row</button>
function projectTable() {
projectRows = projectRows + 1;
var $tbody = $('#project-table').find('tbody');
var $id = $("");
var $tr = $("<tr>");
$id.append(
"<hidden id='projectId'/>" +
"<hidden id='projectVersion'/>"
);
$tr.append(
"<td>" + "<input class='form-control' id='inputProjectName' placeholder='Project Name' type='text'>" + "</td>" +
"<td>" + "<input class='form-control' id='inputClientName' placeholder='Client Name' type='text'>" + "</td>" +
"<td>" + "<input class='form-control' id='inputRate' placeholder='Rate' type='text'></td>" +
"<td>" + "<input type='button' value='Delete' onclick='deleteRow(this)'>" + "</td>");
$tbody.append($id);
$tbody.append($tr);}
projectTable() is called each time I click an Add new Row button. I want to be able to depending on how many rows have been created create an array containining the info from each of the rows like so:
projectList: [
{
id: projectId,
version: projectVersion,
projectName: projectName,
clientName: clientName,
fieldRate: fieldRate
}
but with multiple objects I have tried a few different for loops but they haven't worked for me.
$('#project-table').find('tr').each(...)loops through all rows and you just have to extract the data you need from the<td>elements!?