0

I got some select inputs where I need to create an array of their current selected val. I then need to get these values added together.

My HTML:

<select class="selectName" name="Select options" id="options1">
    <option value="5.0000">10x4</option>
    <option value="20.0000">10x8</option>
</select>

<select class="selectName" name="Select options" id="options2">
    <option value="5.0000">10x4</option>
    <option value="20.0000">10x8</option>
</select>

So far, I can get the current option:selected value by doing:

optionsTotal = $.map($(".selectName").find('option:selected'), function (el, i) {
   return $(el).val();
});

How can I add these values together to get a total? I just want the selected options total.

Here is a fiddle: http://jsfiddle.net/vdecree/e9zxY/34/

3
  • You have two of the same ID, can't do that! Commented Apr 9, 2014 at 14:26
  • possible duplicate of Sum of values in an array using jQuery Commented Apr 9, 2014 at 14:33
  • Yeh that was my bad when i wrote the question, my actual site doesn't have duplicates! Commented Apr 9, 2014 at 14:40

5 Answers 5

0
function getOptionsPrice() {

var optionsTotal = null;

function init() {
    $(".selectName").on('change', function() {

        optionsTotal = new Number(0);
        $.map($(".selectName").find('option:selected'), function (el, i) {
            var v = new Number( $(el).val());
            optionsTotal += v;
        });

        $(".result").text(optionsTotal);

    }).change();
}

  init();
  return optionsTotal;
}

getOptionsPrice();
Sign up to request clarification or add additional context in comments.

Comments

0

I think you need this:

var optionsTotal = 0;
$(".selectName > option:selected").each(function(){
    optionsTotal += Number($(this).val());
});

BTW, you can't have duplicate IDs.

Comments

0

Use parseFloat() inside $.map() and use clouser for optionsTotal function getOptionsPrice() { var optionsTotal = 0;

function init() {
    $(".selectName").on('change', function() {
        optionsTotal = 0;
        $.map($(".selectName").find('option:selected'), function (el, i) {
            optionsTotal += parseFloat($(el).val());
        });

        $(".result").text(optionsTotal);

    }).change();
}

init();
return optionsTotal;
}

getOptionsPrice();

Here is DEMO

Comments

0

Define a variable to hold the total and using $map() iterate through the dropdowns with selected options added upon that total variable as in:

var sum=0;
 $.map( $('.selectName').find('option:selected'),
                          function(el){
                        sum += parseFloat($(el).val());
     });

     console.log(sum);

Comments

0

function getOptionsPrice() {

function init() {
    $(".selectName").on('change', function() {
        var optionsTotal = 0.0;
        $.map($(".selectName").find('option:selected'), function (el, i) {

            optionsTotal += Number($(el).val());
            return optionsTotal;
        });
        $(".result").text(optionsTotal);

    }).change();
}

init();
return optionsTotal;

}

getOptionsPrice();

Working JSFiddle... Check this out...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.