0

I have a string and I want to add one to it rather then it adding a 1 to the end of the string:

Ex. 2 when I add the one it comes out 21 instead I want it to say 3.

Here is the jquery code I am using:

  $(document).ready(function(){

      $("#datepicker1").datepicker({
      showOn: 'focus',
        onClose: function(dateText, inst) {

        var value1 = '1';
            $('#CIY').val(dateText.split('/')[2]);
            $('#CIM').val(dateText.split('/')[0]);
            $('#CID').val(dateText.split('/')[1]);
            $('#COY').val(dateText.split('/')[2]);
            $('#COM').val(dateText.split('/')[0]);
            $('#COD').val(dateText.split('/')[1]);
        },
          onSelect: function( selectedDate ) {
            // Parse the selected date
            var instance = $( this ).data( "datepicker" ),
                date = $.datepicker.parseDate(
                    instance.settings.dateFormat ||
                    $.datepicker._defaults.dateFormat,
                    selectedDate, instance.settings );

            // Add one day
            date.setDate(date.getDate()+1);

            // Set the new date
            $("#datepicker2").datepicker('setDate', date);  

            $("#datepicker2").datepicker();              
        }

      });
      $("#datepicker2").datepicker({
        showOn: 'focus',
        onClose: function(dateText, inst) {
            $('#COY').val(dateText.split('/')[2]);
            $('#COM').val(dateText.split('/')[0]);
            $('#COD').val(dateText.split('/')[1]);
        }
    });

      $("#datepicker1").datepicker('setDate', new Date());
  });

It's the first #COD id that I want to convert to a integer then add one.

Here is the part of the form that the jquery is interacting with:

`

Arrive

Depart

<input type="text" id="COD" value="" name="COD" />

`

2

2 Answers 2

1

use parseInt or parseFloat, e.g.

var x = "1";
var y = parseInt(x, 10) + 1;
console.log(y)  // --> 2
Sign up to request clarification or add additional context in comments.

3 Comments

Would I replace x with: $('#COD').val(dateText.split('/')[1]);
I can't tell. Your snippet doesn't really make sense
parseInt($('#COD').val(dateText.split('/')[1])) + 1; That is what I have and It shoul work I replaced X with $('#COD').val(dateText.split('/')[1], which is the value of that ID. But it only spits out the orignial value not adding one
0
var num = '2';

You can use num = parseInt(num, 10); to tell javascript to parse as a decimal.

Then you can apply any mathematical operation on it.

Rules for parseInt(string, radix)

If the radix parameter is omitted, JavaScript assumes the following:

  • If the string begins with "0x", the radix is 16 (hexadecimal)
  • If the string begins with "0", the radix is 8 (octal). This feature is deprecated
  • If the string begins with any other value, the radix is 10 (decimal)

Working Code:

 <html>
    <head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function(e) {
    var num = $('#cod').val();
    //do something with num
    var str = num.split('/')[1];
     var final = parseInt(str,10)+1; 
    alert(final); //give you 23
    });
    </script>
    </head>
    <body>
    <input value="10/22/2013" id="cod">
    </body>
    </html>

5 Comments

Here is what I have: var num = '2'; parseInt($('#COD').val(dateText.split('/')[1]), 10) + 1; That doesn't add one it just spits out the original number
you are setting value to $('#COD') or getting values; parseInt($('#COD').val(dateText.split('/')[1]), 10) + 1; is looking will give you nothing
Im setting a value to #COD, with that code it gives me the second value of the input datepicker1, EX. 10/22/13, so It gives me 22
Here is a link to the page: 03698f5.netsolhost.com/this when you click on one of the input fields it will give you a calender then the chose of the arrive sets both the select fields, COD is the text field next to the second October select field
That helps, but now how to I attach it to a input?

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.