1
\$\begingroup\$

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?

\$\endgroup\$
2
  • \$\begingroup\$ declare variable passengerId outside of your callback and just do passengerId += "your value" \$\endgroup\$ Commented Mar 15, 2017 at 10:10
  • \$\begingroup\$ Just a very generic tip. When jQuery was released its killer features were CSS selectors and chained selectors, yet newcomers tend to assign IDs to every single element and fetch the item again on each operation. It's also often overlooked that jQuery is still JavaScript and sometimes basic JavaScript features like named functions are neglected. \$\endgroup\$
    – Álvaro González
    Commented Mar 15, 2017 at 10:12

2 Answers 2

0
\$\begingroup\$

You can list the four types of input spinners in an array.

$('.spinner-input, #flight-class').change(function() {
    var ids = ['adult', 'student', 'child', 'baby'];
    var totalCount = ids.reduce((prev, id) => parseInt($(`#${id}-passenger`).val()) + prev , 0);
    var fc = $('#flight-class option:selected').text();

    $('#kisi-sayisi').val(totalCount + ' - ' + fc);
});
\$\endgroup\$
3
  • \$\begingroup\$ this must be "parseInt($(#${id}-passenger)" and its working. thank you for help :) \$\endgroup\$
    – Mustafa Turhan
    Commented Mar 15, 2017 at 10:32
  • \$\begingroup\$ @MustafaTurhan Edited \$\endgroup\$ Commented Mar 15, 2017 at 10:52
  • \$\begingroup\$ Working like a charm. but, How can i set min and max values for each spinner? \$\endgroup\$
    – HDP
    Commented Jan 23, 2020 at 12:56
1
\$\begingroup\$

You could use a jQuery object to query all four of the spinners.

function sum(values) {
    return values.reduce(function (prev, next) { return prev + next; }, 0);
}

function inputToInt(input) {
    return parseInt($(input).val());
}

$('.spinner-input, #flight-class').change(function() {
    var passengerCounts = $('.passenger-list .spinner-input');
    var totalCount = sum(passengerCounts.toArray().map(inputToInt));

    var fc = $('#flight-class option:selected').text();

    $('#kisi-sayisi').val(totalCount + ' - ' + fc);
});
\$\endgroup\$
3
  • \$\begingroup\$ i get it and it's working. thank you very much :) \$\endgroup\$
    – Mustafa Turhan
    Commented Mar 15, 2017 at 10:31
  • \$\begingroup\$ How can i set min and max values for each spinner? \$\endgroup\$
    – HDP
    Commented Jan 23, 2020 at 13:03
  • \$\begingroup\$ @HDP That would depend on the kind of spinner you're using. I'm afraid I'm not very familiar with spinners, though. \$\endgroup\$
    – JLRishe
    Commented Jan 23, 2020 at 15:30