I've been trying to figure this out, and have been having a lot of trouble so I'd appreciate the help. This is the situation. I have a dynamically generated table, and for example, lets say that there are ten rows in this dynamically generated table from data obtained from a database. I'm using this code to get the data from the database using JSON.
$(document).ready(function() {
$("#findtrucker-btn").click(function(event){
$.getJSON("/getallLoadsA", function(loads) {
var tr = loads;
for (var i = 0; i < loads.length; i++) {
tr = $('<tr/>');
tr.append("<td>" + i + "</td>");
tr.append("<td>" + loads[i].loadname + "</td>");
tr.append("<td>" + loads[i].category + "</td>");
tr.append("<td>" + loads[i].space + "</td>");
tr.append("<td>" + loads[i].weight + "</td>");
tr.append("<td>" + loads[i].loadprice + "</td>");
tr.append("<td>" + loads[i].street + " " + loads[i].city + ", " + loads[i].state + "</td>");
tr.append("<td>" + loads[i].dstreet + " " + loads[i].dcity + ", " + loads[i].dstate + "</td>");
tr.append("<td>" + loads[i].status + "</td>");
tr.append("<td><button onclick=update()>Update</button></td>");
$('#table').append(tr);
}
});
});
});
So, I have 10 rows in my table, and each row has a button with name "update" that when clicked, I want to go to a different page. I've found that using the following will get me the desired output and page load, but only for row 1.
window.location.href = '/updateload?loadname='+(document.getElementById("table").rows[1].cells.item(1).innerHTML);
I want to be able to, from clicking the button "update" on each row, access that particular row's 2nd value so that loadname='that particular row's loadname' or loads[i].loadname where i = the row I'm currently on for that particular button and switch pages appropriately where upon switching pages, I will be able to get data for that row and be able to update data subsequently. Also, data is being appended to this table:
<table id="table" class="table table-bordered table-striped">
<tr>
<th>#</th>
<th>Loadname</th>
<th>Category</th>
<th>Space</th>
<th>Weight</th>
<th>Load Price</th>
<th>Pickup Location</th>
<th>Deliver Location</th>
<th>Status</th>
<th>Update</th>
</tr>
</table>