2

I am trying to get the month that is 3 months from a selected month using a html select box and jquery in the below code. The code is not adding two variables together instead its treating them as two strings. Could anyone assist me to get this right?

<body>
    <form role="form" class="form-inline">
        <div class="form-group">
            <label class="control-label" for="Q1Month">
                Quarter 1 Month
            </label>
            <select class="form-control" id="Q1Month">
                <option selected value=''>--Select Month--</option>
                <option value='1'>January</option>
                <option value='2'>February</option>
                <option value='3'>March</option>
                <option value='4'>April</option>
                <option value='5'>May</option>
                <option value='6'>June</option>
                <option value='7'>July</option>
                <option value='8'>August</option>
                <option value='9'>September</option>
                <option value='10'>October</option>
                <option value='11'>November</option>
                <option value='12'>December</option>
            </select>
        </div>
    </form>
    <script src="js/jquery.js"></script>
    <script>
    $(function() {
        $('#Q1Month').change(function() {
            var month = $(this).val();
            $('#Q2Month').val(month);
            var advanceby = 3;
            var newmonth = month + advanceby;
            window.alert(newmonth);
        });
    });
    </script>
</body>
Tushar
  • 85,780
  • 21
  • 159
  • 179
Gathua
  • 81
  • 1
  • 6

3 Answers3

2

When any value is read from the DOM, it is string. To convert it to the Number you can use unary + operator.

Add + in front of the value

var month = +$(this).val();

+ is also the concatenation operator in Javascript, so when any one of the operand is string + is used for string concatenation.

$(function() {
  $('#Q1Month').change(
    function() {
      var month = $(this).val();
      $('#Q2Month').val(month);
      var advanceby = 3;
      var newmonth = month + advanceby;
      window.alert(newmonth);
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<form role="form" class="form-inline">
  <div class="form-group">
    <label class="control-label" for="Q1Month">
      Quarter 1 Month
    </label>
    <select class="form-control" id="Q1Month">
      <option selected value=''>--Select Month--</option>
      <option value='1'>January</option>
      <option value='2'>February</option>
      <option value='3'>March</option>
      <option value='4'>April</option>
      <option value='5'>May</option>
      <option value='6'>June</option>
      <option value='7'>July</option>
      <option value='8'>August</option>
      <option value='9'>September</option>
      <option value='10'>October</option>
      <option value='11'>November</option>
      <option value='12'>December</option>
    </select>
  </div>
</form>
Tushar
  • 85,780
  • 21
  • 159
  • 179
0

Because the value that you have fetched is a string! What You need is the option index and the actual value, so try accessing the current selected options index instead of value and then add 3 and reset your required element. Hope I helped

Shambhavi
  • 51
  • 5
0

val() always returns string, so use parseInt() to convert string to integer before adding

$(function() {
  $('#Q1Month').change(
    function() {
      var month = parseInt($(this).val(),10);
      $('#Q2Month').val(month);
      var advanceby = 3;
      var newmonth = month + advanceby;
      window.alert(newmonth);
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<form role="form" class="form-inline">
  <div class="form-group">
    <label class="control-label" for="Q1Month">
      Quarter 1 Month
    </label>
    <select class="form-control" id="Q1Month">
      <option selected value=''>--Select Month--</option>
      <option value='1'>January</option>
      <option value='2'>February</option>
      <option value='3'>March</option>
      <option value='4'>April</option>
      <option value='5'>May</option>
      <option value='6'>June</option>
      <option value='7'>July</option>
      <option value='8'>August</option>
      <option value='9'>September</option>
      <option value='10'>October</option>
      <option value='11'>November</option>
      <option value='12'>December</option>
    </select>
  </div>
</form>
Rolwin Crasta
  • 4,219
  • 3
  • 35
  • 45