I'm new person who working on jQuery and I need your help.
$('.spinner-input, #flight-class').change(function() {
var ap = parseInt($('#adult-passenger').val());
var sp = parseInt($('#student-passenger').val());
var cp = parseInt($('#child-passenger').val());
var bp = parseInt($('#baby-passenger').val());
var fc = $('#flight-class option:selected').text();
var totalCount = ap + sp + cp + bp;
$('#kisi-sayisi').val(totalCount + ' - ' + fc);
});
You can see my code in here
And working demo is here
So, here is the situtation: There is nothing bad about this code, its working normally when you click "number of people" in form. You can see, if you click minus or plus sign, form is updating itself. But when I look this code, I'm feeling like repeating myself.
When I tried turn this code to DRY format, i do this:
('.spinner-input, #flight-class').change(function() {
var passengerId = parseInt($(this).val());
});
Now I get value of input. I need to get sum of values but I can't because something is missing here.
How can I DRY this code?
passengerId
outside of your callback and just dopassengerId += "your value"
\$\endgroup\$