I am looking for a way to read the value of an dynamically created input field and then get the average of them. I've got a tablerow with multiple inputs, and I want to add all values of those inputs together and show them at the end of the row.
What I got so far:
Html (I am generating such rows in my table):
<tr>
<td><input type='number' class='userInput' name='userInput'></td>
<td><input type='number' class='userInput' name='userInput'></td>
<td><input type='number' class='userInput' name='userInput'></td>
<td class='avg'>X</td> <!-- Average -->
</tr>
My jQuery code looks like this:
$(document).on('input', '.userInput', function () {
var parentRow = $(this).parents('td');
var inputs = parentRow.children('input');
var avgText = $(this).parents('tr').find('.avg');
var avg = 0;
inputs.each(function () {
avg += parseInt(this.value);
});
avg /= 3;
avgText.text(avg);
});
My Problem with this code is, that it doesnt update the total, it just overwrites it when I write something into the second inputfield. So what I am looking for, is a way to just update the average.