PDA

View Full Version : Cast string as int in Javascript


expinch
08-04-2004, 09:04 AM
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:

<input name="currentWeek" type="hidden" value="$week">

and another is contained in a select box:

<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.

RysChwith
08-04-2004, 10:23 AM
var finalWeek = parseInt( currentWeek ) + parseInt( weekSpan );

Rys

expinch
08-04-2004, 10:51 AM
That's what I was looking for! Time to get a "non-beginner" JS book. Thanks.

afterburn
08-04-2004, 02:59 PM
http://devedge.netscape.com is the best place for documentation about javascript as they created it.

ucm
08-04-2004, 06:08 PM
Originally posted by expinch
That's what I was looking for! Time to get a "non-beginner" JS book. Thanks.

Visual Blue Print Series (http://www.amazon.com/exec/obidos/search-handle-url/index%3Dstripbooks%26field-keywords%3Dvisual%252520blueprint%26store-name%3Dbooks/104-1860356-0211161)

you can't go wrong, I read the JavaScript (http://www.amazon.com/exec/obidos/tg/detail/-/0764547305/qid=1091657090/sr=1-1/ref=sr_1_1/104-1860356-0211161?v=glance&s=books) 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 >-)

afterburn
08-04-2004, 09:45 PM
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.

ucm
08-10-2004, 11:59 AM
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