Click to See Complete Forum and Search --> : strings attached


okham
11-06-2003, 02:59 PM
Hey folx, how do I convert a javascript variable into a string? The value is a [selectedIndex] from an option...

okh

AdamGundry
11-06-2003, 03:06 PM
You just treat it as such, and the interpreter will perform an implict toString() call to convert in when necessary. Javascript has loosely defined types, and hence conversions are simple.

You may have problems with differentiating between the add and concatenate (glue strings together) operators, but if you need to concatenate just add a null string (e.g. '12' + '34' + '' returns '1234').

If you want to get the currently selected value from a <select> element, you can use this (replace form and select with appropriate names):
document.form.select.options[document.form.select.selectedIndex].value

Adam

okham
11-06-2003, 03:33 PM
I think I understand what you're saying, Adam. Could be that's how it's interpreted in javascript... the problem I've got is regarding the PayPal server, though; for instance, last night I had to convert a javascript number into a text string so their server would accept it. Now I've got the problem sending some text info in the same form because it's a variable in javascript. - - You mentioned toString()...

okh

AdamGundry
11-06-2003, 03:35 PM
If you are submitting to PayPal, I guess you need to pass your JS variables in form fields. You can set hidden fields up like this:

<input type="hidden" name="example" value="test">

You can then change them like this:

document.formname.example.value = myVariable; // myVariable automatically converted to string.

Adam

okham
11-06-2003, 03:46 PM
..yeah, it's a been a struggle working things out, but I'll eventually get it. Your example has worked for me passing one variable in an event like onsubmit, but for some reason it won't accept it when I apply the same variable inside a function where I need to add several strings and then pass them as one string. Just a quirk I gotta work out... feel free to make any other suggestions.

okh