I have 2 types of text inputs, positive and questionable. There are multiple sets of these inputs that I need to show the total after adding all the numbers entered into both types of inputs. eg: 2 positive + 2 questionable = Total: 4
Then, I need to find the percentage of positive vs questionable from that total.
eg: 50% positive and 50% questionable.
I want to add these totals and percentages up live as the user enters them.
So far I am not getting anywhere. Any ideas for the best way to do this? Should I use .blur or .each?
jsfiddle: http://jsfiddle.net/infatti/UjU4a/
Here is what I have so far but it is not working:
<label>How many positive?</label>
<input type="text" name="positive" class="observationPositive">
<br />
<label>How many questionable?</label>
<input type="text" name="questionable" class="observationQuestionable">
<br />
<hr />
<label>How many positive?</label>
<input type="text" name="positive" class="observationPositive">
<br />
<label>How many questionable?</label>
<input type="text" name="questionable" class="observationQuestionable">
<br />
<hr />
<h4><span id="observationTotal"></span> Total</h4>
<h4><span id="observationQuestionablePercentage"></span>% Questionable</h4>
<h4><span id="observationPositivePercentage"></span>% Positive</h4>
$('.observationPositive').blur(function () {
var sum = 0;
$('.observationPositive').each(function() {
sum += Number($(this).val());
});
});
$('#observationTotal').text(sum);
Assuming you mean “average of the values”. However, I could do with understanding more about what it is you are trying to achieve – you might want to pair up each positive input with a questionable input…