javascript - jQuery Add row to table with dynamic ids' -
function add_row_retail() { $(document).ready(function () { var table = document.getelementbyid("retail"); var row = table.insertrow(-1); var row_id = $('#retail').val('tr[id]:last'); console.log(row_id); var cell_init = row.insertcell(-1); cell_init.innerhtml = "blank"; }); }
i trying id of table row(<tr>
) before added row, , add 1 this, proper parseint(...). doing same cell (<td>
) next, every cell has unique id each table. can't seem find row's id.
here "correct" code question
function add_row_retail() { var table = document.getelementbyid("retail"); // row id var row_id = $('#retail tr:last').attr('id'); var row = table.insertrow(-1); var next_row_id = "tr_" + (1+parseint(row_id.match(/\d+/)[0],10)); $(row).attr('id', next_row_id); (var = 0; < 8; i++) { var cell_id = $('#retail td:last').attr('id'); console.log(cell_id); var next_cell_id = "td_" + (1+parseint(cell_id.match(/\d+/)[0],10)); console.log(next_cell_id); var cell = row.insertcell(-1); $(cell).attr('id', next_cell_id); $(cell).innerhtml = "blank"; } }
rather $('#retail').val('tr[id]:last');
think want:
var row_id = $('#retail tr:last').attr('id')
this selector finds last tr under #retail
element, , returns id
attribute.
jquery last selector: http://api.jquery.com/last-selector/
next problem: ids cannot start numbers. rename ids "tr_1", "tr_2", etc.
next problem: extract numbers string:
"tr_123".match(/\d+/)[0]; // returns 123.
add 1 it:
var next_id = "tr_" + (1+parseint("tr_123".match(/\d+/)[0],10));
then, set new id.
var row = table.insertrow(-1); ... $(row).attr('id', next_id);
Comments
Post a Comment