Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

passing a string as a variable

Status
Not open for further replies.

Pryer

Programmer
Oct 24, 2005
12
0
0
US
I am having a problem with passing a string as a variable. Let me do a bit of explaining on why to avoid any confusion. I have several flash objects on my page all named and identified differently (ex. 'OnTime', 'ncr', 'intq', 'exq'). I have a js function that dynamically updates the xml content these flash objects are reading. Below is how/what i am passing.

<input type='button' value='Go' onClick='javascript:updateCXML(ObjectName, JS var containing XML);'

Here is where my problem comes in. I have a select box that users choose from to update the Flash object (ex. jan, feb, mar). When I populate the select box using ASP(vbscript) the value of the options are actually names of js variables (ex. OnTimemarch2006, ncrmar2006). Everything works as expected except when i click the 'Go' button i pass the name/id of the Flash object, then i use "javascript:updateCXML(ObjectName, document.getElementByID('select box ID').value);"

This passes a string to the function, when I want it to actually pass a variable therefore my function does not work. How can I convert a string to a variable? I hope this makes since. The actual code is below.


Code:
[/b]

<select id="otd">				
<option value="strxmlontimejanuary2006">january</option>
<option value="strxmlontimefebruary2006">february</option>
<option value="strxmlontimemarch2006"selected>march</option>
</select> 

<input value="Go" type="button" onClick="javascript:setFCNewData('OnTime', [b]document.getElementById('otd').value);[/b]" />

[b][Code end][/b]

Thank you in advance for any feedback.
 
I'm going to simplify this a bit, so it's easier to explain (for me).
{HTML}
Code:
<select id="selbox">
   <option value="v1">January</option>
   <option value="v2">February</option>
   <option value="v3">March</option>
</select>
<input value="Go" type="button" onclick="getValue(document.getElementById('selbox').value);"/>
{JavaScript}
Code:
var myvars = new Array();
myvars.v1 = "This is value 1 (Jan)";
myvars.v2 = "This is value 2 (Feb)";
myvars.v3 = "This is value 3 (Mar)";
function getValue(aValue) {
   window.alert("The value is\n" + myvars[aValue]);
   return;
}
By placing the variables in an array we can references them using the [tt][][/tt] subscripting operator, because [tt]myvars.x[/tt] is exactly the same as [tt]myvars["x"][/tt] (note that it's not the same as [tt]myvars[x][/tt]).
Hope this is clear.

[tt]_______________________________________
Roger [pc2]
The Eileens are out there[/tt]
 
woja-

This does work, but creates another problem than i didn't explain earlier. I am creating the js var with asp depending on what data i retreive from a database. So if i have a in my database records from jan-july, i need to create variables for each month. Does this make any since??

 
So, does your ASP does something like this (assuming VBScript)?
Code:
<script type="text/javascript">
<%
  Set rs = db.OpenRecordSet("....")
  While (Not rs.EOF)
%>
   var <%=rs.Field("Name")%> = "<%=rs.Field("Value"%>";
<%
     rs.MoveNext
  Wend
%>
</script>
If so, you should be able to do this:
Code:
<script type="text/javascript">
  var myvars = new Array();
<%
  Set rs = db.OpenRecordSet("....")
  While (Not rs.EOF)
%>
   myvars.<%=rs.Field("Name")%> = "<%=rs.Field("Value"%>";
<%
     rs.MoveNext
  Wend
%>
</script>
Hope this helps

[tt]_______________________________________
Roger [pc2]
The Eileens are out there[/tt]
 
i guess its the eval() function that u want:

TheVar="ASD"

TheVariable="TheVar"
alert(eval(TheVariable))

Known is handfull, Unknown is worldfull
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top