2

I am aiming to create a form to generate a quick quote. The ambition is that theu ser is able to select computer parts with a value assigned to each > .

For instance,

<form id="formsum">
  <select>
  <option value="1">1</option>
  <option value="5">2</option>
  <option value="2">3</option>
  </select>
</form>

<form id="formsum2">
  <select>
  <option value="2">1</option>
  <option value="10">2</option>
  <option value="4">3</option>
  </select>
</form>

So if the user selects option 2 on formsum and then 2 on formsum2 Id like the function to read "15".

3
  • so where you have written your js code? Commented Oct 24, 2017 at 10:56
  • And the problem/question is? Commented Oct 24, 2017 at 10:57
  • I started a codepen but it wasnt working so i deleted it. If someone could point me in the right direction that would be great :) im fairly new to this so im not sure what search terms to use or jquery terms to use. Commented Oct 24, 2017 at 10:58

3 Answers 3

3

You can try something like this:

$("[id^=formsum] select").change(function() {
  var sum = 0;
  $("[id^=formsum] select").each(function() {
    sum += Number($(this).val());
  });
  console.log(sum)
})

Demo

$("[id^=formsum] select").change(function() {
  var sum = 0;
  $("[id^=formsum] select").each(function() {
    sum += Number($(this).val());
  });
  console.log(sum)
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="formsum">
  <select>
  <option value="1">1</option>
  <option value="5">2</option>
  <option value="2">3</option>
  </select>
</form>

<form id="formsum2">
  <select>
  <option value="2">1</option>
  <option value="10">2</option>
  <option value="4">3</option>
  </select>
</form>

Sign up to request clarification or add additional context in comments.

3 Comments

@pedram what do you mean, if i select 2 in both it returns 15
@Jackowski The OP says ` function to read "15"` when option 2 in both are select, aka mine does that. So why do you think the code is wrong, i have used the same code as the OP
@CarstenLøvboAndersen i know sorry, i missunderstood the question. I deleted my previous comment.
2

Get Selected value of select and add it.

Use parseInt to parse selected value into int.

$('#btn').on('click',function(){
   var sum  = parseInt($('#formsum select').val()) + parseInt($('#formsum2 select').val());
   console.log(sum);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="formsum">
  <select>
  <option value="1">1</option>
  <option value="5">2</option>
  <option value="2">3</option>
  </select>
</form>

<form id="formsum2">
  <select>
  <option value="2">1</option>
  <option value="10">2</option>
  <option value="4">3</option>
  </select>
</form>

<input type="button" id="btn" value="show"/>

2 Comments

Thank you very much, looks like i need to read up on parseint!
@jbibb917 : hope it solved your problem. then accept it as answer.
1

Try this out on both dropdown change event

$(document).ready(function() {
    $("#formsum,#formsum2").change(function() {
    var sum  = parseInt($('#formsum select').val()) + parseInt($('#formsum2 select').val());
    alert(sum);
    });
});

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.