0

I want to: Get value from select, sum with other values, and place it inside a span.

Can I send the result of a value to a div/span without having to submit with a button? Is that possible?

please look at my fiddle: https://jsfiddle.net/groseler/3mq1ye1y/

HTML

$(function () {
  $("#field16-1").change(function() {
    var val = $(this).val();
    var comEx = "150";
    var youngDriver = "100"; 
    var resultSum = parseInt(val, 10) + parseInt(comEx, 10) + parseInt(youngDriver, 10);
     
  });
  $("#result").html(resultSum);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="field16-1">
  <option value="">Please select...</option>
  <option value="120">120</option>
  <option value="230">230</option>
  <option value="260">260</option>
 </select>
 <div>
 Result should appear here --->  <span id="result" ></span></div>

4 Answers 4

1

Your code works after a bit change. You have only some typo. You need to add the $("#result").html(resultSum) into the change callback.

You don't need to submit anything. Submit is for the forms and after submitting the data almost always go to the server side code

$(function () {  
  $("#field16-1").change(function() {
    var val = $(this).val();
    var comEx = "150";
    var youngDriver = "100"; 
    var resultSum = parseInt(val, 10) + parseInt(comEx, 10) + parseInt(youngDriver, 10);
     $("#result").html(resultSum);
  });
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="field16-1">
  <option value="">Please select...</option>
  <option value="120">120</option>
  <option value="230">230</option>
  <option value="260">260</option>
 </select>
 <div>
<span id="result" ></span></div>

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

2 Comments

@groseler Be a bit attentive :). Your code is working
You right, I have to be. Already too many hours in front the pc... Thank you one more time ;)
1

Put $("#result").html(resultSum); inside the function

$(function () {
  $("#field16-1").change(function() {
    var val = $(this).val();
    var comEx = "150";
    var youngDriver = "100"; 
    var resultSum = parseInt(val, 10) + parseInt(comEx, 10) + parseInt(youngDriver, 10);
      $("#result").html(resultSum);
  });

});

JSFIDDLE

Comments

1

The mistake you have done is that you put $("#result").html(resultSum); this outside the change event of dropdown.

Comments

0

Move $("#result").html(resultSum); statement to within .change() so the the sum can be changed every time the value of the select element changes.

$(function () {
  $("#field16-1").change(function() {
    var val = $(this).val();
    var comEx = "150";
    var youngDriver = "100"; 
    var resultSum = parseInt(val, 10) + parseInt(comEx, 10) + parseInt(youngDriver, 10);
    $("#result").html(resultSum);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="field16-1">
  <option value="">Please select...</option>
  <option value="120">120</option>
  <option value="230">230</option>
  <option value="260">260</option>
 </select>
 <div>
 Result should appear here --->  <span id="result" ></span></div>

1 Comment

Thank you for your time! I've already used the @SurenSrapyan answer.

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.