HTML:
<table id="math-table">
<tr>
<td><input type="text" id="A1" name="A" value=""></td>
<td><input type="text" id="B1" name="B" value=""></td>
<td><input type="text" id="C1" name="C" readonly="readonly" tabIndex="-1" value=""></td>
</tr>
</table>
JS:
$("#math-table input").live("keyup", function(){
var id = this.id.match(/\d+/);
$("#C"+id).val( Math.round (($("#A"+id).val() / $("#B"+id).val()) * 100) + "%" );
$('#A'+id).attr('value', $('#A'+id).val());
$('#B'+id).attr('value', $('#B'+id).val());
$('#C'+id).attr('value', $('#C'+id).val());
});
var uniqueIds = $("#math-table tr").length;
$("#math-table input[id^='B']").live("change", function(){
var $thisRow = $(this).closest("tr"),
$clone = $thisRow.clone(), // Clone row
$inputs = $clone.find("input").val("");// Reset values, return all inputs
uniqueIds++; //Increment ID
$inputs[0].id = "A" + uniqueIds;
$inputs[1].id = "B" + uniqueIds;
$inputs[2].id = "C" + uniqueIds;
$thisRow.after($clone);
});
You can see that A/B = C% Pretty simple. How would I add a different CSS class to only C based on a certain %?
Red 1-33%
Green 34-66%
Blue 67-100%
I updated your jsFiddle:
http://jsfiddle.net/CzZxf/13/
I added a variable
percentand set the background color based on that like this:EDIT: I updated the script to remove the class from the clone. Add this