I have created a table and I can add and remove rows using jQuery when a checkbox is checked. My question here is, how can I add an index number at the first column dynamically?
My table:
<div>
<table class="config" id="status_table">
<thead>
<tr>
<th colspan="4"; style= "padding-bottom: 20px; color:#6666FF; text-align:left; font-size: 1.5em">Output Status</th>
</tr>
<tr>
<th>Index</th>
<th>Output Type</th>
<th>Output Number</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<tr>
<td >1</td>
<td ><p id="type_row_one"></p></td>
<td ><p id="number_row_one"></p></td>
<td>
<img class="image" id = "off" style="margin-left:20px" src="static/OffLamp-icon.png" >
<img class="image" id = "on" style="margin-left:20px" src="static/OnLamp-icon.png" >
</td>
</tr>
</tbody>
</table>
</div>
My jQuery:
$('#outputCB').change(function(){
if($('#outputCB_1').is(':checked')){
$('#status_table tr:last').after('<tr id="output_newrow_1"><td>index+1</td><td>testing</td></tr>');}
else{
$('#status_table').find("#output_newrow_1").remove();
}
});
So instead of "index+1" I want it to be able to increment from the previous index number, and decrease if the row is removed.
How should I go about doing it? Can I assign a variable and ++1 or --1 at the Javascript? I don't really know where to start....
UPDATE:
I tried this but failed:
var index=1;
$('#outputCB').change(function(){
if($('#outputCB_1' ||'#outputCB_2').is(':checked')){
index++;
$('#status_table tr:last').after('<tr id="output_newrow_'+index+'"><td>'+index+'</td><td id="type_row_'+index+'">type_row_'+index+'</td><td id="num_row_'+index+'">num row '+index+'</td><td><img class="image" src="static/OffLamp-icon.png" style="height:64px; width=64px"/></td></tr>');
}
else{
index--;
$('#status_table').find("#output_newrow_'+index+'").remove();
}
});
it will never remove anything and it added 2 rows at a time when #outputCB_1 is checked again. :/ Why so? (sorry this is too long for a comment.)