I stuck to this problem. I want to calculate body BMI.
My HTML – every row <tr> is created dynamically by PHP.
<tbody>
<tr id="person_bmi">
<td>Jack</td>
<td><input name="weight" type="text" class="span1" id="weight" maxlength="5"></td>
<td><input name="height" type="text" class="span1" id="height" maxlength="5"></td>
<td><input name="bmi" type="text" class="span1" id="bmi" readonly></td>
</tr>
<tr id="person_bmi">
<td>Michael</td>
<td><input name="weight" type="text" class="span1" id="weight" maxlength="5"></td>
<td><input name="height" type="text" class="span1" id="height" maxlength="5"></td>
<td><input name="bmi" type="text" class="span1" id="bmi" readonly></td>
</tr>
//and so on...
</tbody>
What I try to achieve is every person Jack, Michael, so on.. will have their own BMI values in #bmi textfield after calculated their own $weight/($height*100/$height*100) value. I try to create my own code but its not make sense..
$("#weight, #height").keyup(function(){
var $weight= $("#weight").val();
var $height= $("#height").val();
var $bmi = $weight/($height/100*$height/100);
$("#bmi").val(parseInt($bmi));
});
Can someone show me the way? Thanks
Remove the duplicate IDs and use
class="weight",class="height"andclass="bmi"instead. Then you can use the following jQuery code:If you want to allow floats for the input values, replace the two lines containing
parseIntwith these:Demo: http://jsfiddle.net/KStjd/