Announcement

Collapse
No announcement yet.

Cast string as int in Javascript

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Cast string as int in Javascript

    I have two variables that I'm sending in a form to another php page. One of the variables is sent in a hidden form:

    PHP Code:
     <input name="currentWeek" type="hidden" value="$week"
    and another is contained in a select box:

    PHP Code:
    <select name="weekSpan"><option value="1">1</option><option value="2">2</option>
     <
    option value="3">3</option><option value="4">4</option><option value="5">5</option></select
    I'm assuming that all data send through an html form is converted to a string in Javascript because they're all contained in quotes??

    The "currentWeek" variable contains the present week and "weekSpan" holds the total weeks the data is valid. I read the values out fine...

    var weekSpan = document.forms[0].weekSpan.value;
    var currentWeek = document.forms[0].currentWeek.value;
    var finalWeek = currentWeek+weekSpan;

    I want to "confirm" that the data is correct...

    var confirmed = confirm('Are you sure you want this draft sheet to last from week '+currentWeek+' to week '+finalWeek+'?');

    ..but as you can guess the finalWeek value is just the two values contatenated, not added together. I have no idea how to convert these values to ints, or if there is some way to send them as ints with the form? Thanks.

  • #2
    var finalWeek = parseInt( currentWeek ) + parseInt( weekSpan );

    Rys

    Comment


    • #3
      That's what I was looking for! Time to get a "non-beginner" JS book. Thanks.

      Comment


      • #4
        http://devedge.netscape.com is the best place for documentation about javascript as they created it.
        www.shapemetrics.com
        Red GateSQL tools for DBA

        Comment


        • #5
          Originally posted by expinch
          That's what I was looking for! Time to get a "non-beginner" JS book. Thanks.
          Visual Blue Print Series

          you can't go wrong, I read the JavaScript one cover to cover in a week and a half then built tetris from my head over the next week and a half... ohhh yeahhh >-)

          Comment


          • #6
            I can never seem read a book, my issue is most are just boring me with useless information. I just get to what i need then sell the book. references I just use websites.
            www.shapemetrics.com
            Red GateSQL tools for DBA

            Comment


            • #7
              Originally posted by afterburn
              I can never seem read a book, my issue is most are just boring me with useless information. I just get to what i need then sell the book. references I just use websites.
              then you'll absolutely love the Visual Blueprinr series... all code and no crap! :-D :-D :-D

              Comment

              Working...
              X