Ilanus
Ilanus

Reputation: 6928

Calculate all numbers from a Multiple selection return the total sum Multiselect + jQuery

I am trying to sum multiple values into one value and append it to input value. jQuery UPDATED:

afterSelect: function(value){
        $.ajax({
            type: 'GET',
            url: '/police/get_res_price?price=' + value,
            success: function (data) {
                var initial_price = $('.give-me-money').val();
                var obj = JSON.parse(data);
                $.each(obj, function(booking_price, value) {
                    initial_price += value.BOOKING_PRICE;
                });
                $('.give-me-money').val(initial_price); //set total
                console.log(initial_price);
            }
        });
        this.qs1.cache();
        this.qs2.cache();
    },

HTML:

<select id='custom-headers' multiple='multiple' class="searchable">
<?php foreach ($get_reservations as $res_option): ?>
<option value="<?php print $res_option->DBASE_ID; ?>"><?php print $res_option->DBASE_ID; ?></option>
<?php endforeach; ?>
</select>

<input class="give-me-money" type="text">

Each click log me number for example, 5117, 547, 987, 54. and appends it to the input upon the last selection of the multiselect. i want someway to say 'wait' sum 5117+547+987+54 and append 6705 to the input value, how would i do that?

Upvotes: 2

Views: 714

Answers (2)

Niki van Stein
Niki van Stein

Reputation: 10734

You need to add all values to the same variable and then set the value to the .give-me-money field. Also change your html:

html

<input class="give-me-money" type="text" value="0">

javascript

afterSelect: function(value){
    $.ajax({
        type: 'GET',
        url: '/police/get_res_price?price=' + value,
        success: function (data) {

            var initial_price = parseInt($('.give-me-money').val());
            var obj = JSON.parse(data);
            $.each(obj, function(booking_price, value) {
                console.log(value.BOOKING_PRICE);
                initial_price += parseInt(value.BOOKING_PRICE);
            });
            $('.give-me-money').val(initial_price); //set total
        }
    });
    this.qs1.cache();
    this.qs2.cache();
}

Upvotes: 1

Shilly
Shilly

Reputation: 8589

Showing the code from chat:

afterSelect: function(value){
            $.ajax({
                type: 'GET',
                url: '/police/get_res_price?price=' + value,
                success: function (data) {
                    var initial_price = parseInt($('.give-me-money').val(), 10) || 0;
                    var obj = JSON.parse(data);
                    $.each(obj, function(booking_price, value) {
                        initial_price += parseInt(value.BOOKING_PRICE, 10);
                    });
                    $('.give-me-money').val(initial_price); //set total
                    console.log(initial_price);
                }
            });
            this.qs1.cache();
            this.qs2.cache();
        },

Upvotes: 2

Related Questions