I am once again stuck with a jQuery issue, and I'm hoping for some help.
I have a series of checkboxes that look like these two:
<input type="checkbox" name="MultiPointInspection" id="MultiPointInspection" class="MultiPointInspection" value="<?php echo $MultiPointInspection; ?>" onKeyPress="return disableEnterKey(event)" rel="Multi Point Vehicle Inspection" /> Multi Point Vehicle Inspection<br />
<input type="checkbox" name="TireRotationandBrake" id="TireRotationandBrake" class="TireRotationandBrake" value="<?php echo $TireRotationandBrake; ?>" onKeyPress="return disableEnterKey(event)" /> Tire Rotation and Brake Inspection<br />
What I want to do is add a new dynamic row to a previous table (which I can already do with a separate button), but in this case, add the name, or id or class name (which are all the same as per above) as a textbox value in that row when any of these are clicked. I also want them to remove when they are unclicked, but they won't always be the last row, so :last doesn't really work for me.
This is the previous table:
<table id="atable" class="atable">
<tr>
<td>Description</td><td>Stocked</td><td>Quantity</td><td>Part Price</td><td>Hours</td><td>Rate Class</td><td>Total</td><td>Approved</td><td>Remove Row</td></tr>
<tr class="dynamic_row">
<td><input name="description[]" id="description" value="<?php echo $description; ?>" size="55" class="numeric add_to_total description" onKeyPress="return disableEnterKey(event)" />
</td><td align="center"><input type="checkbox" name="stocked[]" id="stocked" value="<?php echo $stocked; ?>" size="5" class="numeric add_to_total" onKeyPress="return disableEnterKey(event)" />
</td><td><input name="quantity[]" id="quantity" value="<?php echo $quantity; ?>" size="5" class="numeric add_to_total quantity" onKeyPress="return disableEnterKey(event)" />
</td><td><input name="partPrice[]" id="partPrice" value="<?php echo $partPrice; ?>" size="10" class="numeric add_to_total part" onKeyPress="return disableEnterKey(event)" />
</td><td><input name="hours[]" id="hours" value="<?php echo $hours; ?>" size="10" class="numeric add_to_total hours" onKeyPress="return disableEnterKey(event)" />
</td><td><select name="rate[]" id="rate" class="numeric add_to_total rate" onKeyPress="return disableEnterKey(event)">
<?php
//get rate options from database
?>
</select>
</td><td><input name="total[]" id="total" size="10" class="numeric is_total" readonly="readonly" onKeyPress="return disableEnterKey(event)" />
</td><td align="center"><input type="checkbox" name="approved[]" id="approved" class="add_to_total approved" checked="checked" value="<?php echo $approved; ?>" size="10" onKeyPress="return disableEnterKey(event)" />
</td><td align="center"><img src="img/x.png" width="25" height="25" id="removeButton" title="Remove Row" class="delRow" />
</td></tr>
<img src="img/plus.png" width="25" height="25" id="btnAddRow" title="Add New Row" class="alternativeRow" />
</table>
I am using the jQuery Table AddRow plugin. I have used the following code to make it add the row, which is fine (although I don't get my pretty alternating rows), but it also adds the row to the bottom, below my "add row" button, so I'm also looking for a solution to the appendTo(), and would rather have it added just above the last row:
$("#MultiPointInspection,#TireRotationandBrake").on('click', function() {
if ($(this).prop("checked")) {
$('#atable .dynamic_row:first').clone().appendTo("#atable");
}
else {
$('#atable .dynamic_row:last').remove();
}
});
and this is the regular code to add a row on the other button click:
$("document").ready(function(){
$(".alternativeRow").btnAddRow({oddRowCSS:"oddRow",evenRowCSS:"evenRow"});
$(".delRow").btnDelRow();
});
As if I'm not asking enough, what I would also like to do is not have the last column added for these rows. The last column contains a link to remove the row, and I would only like the row to be removed on the unclick of the checkbox.
I have created a fiddle with the basic setup of the problem (note the javascript contains the full jQuery Table AddRow plugin code), and would appreciate any help I could get with making this work.
Thank you.