Click to See Complete Forum and Search --> : Setting a variable's value to *various* variables' values...


spitrhyma
08-19-2005, 01:20 AM
It's really hard to explain but I want to use the value of 2 variables, n use those 2 variables values to name another variable:

VarA="Goodness gracious!";
VarB="Var";
VarC="A";
VarD=VarB+VarC;
alert(VarD);

Instead of the alert popping up and saying "VarA" I want it to say "Goodness gracious!"...

NOTE: IF YOU CAN HELP ME WITH THIS PROBLEM THEN THAT IS ALL I WILL NEED -- THE REST WON'T BE NEEDED


Now to be more exact with what I'm trying to do... I want to:

- Create variables and set a unique message as their value

IE:
var DT_A="PACKAGE A INCLUDES THIS N THAT";
var DT_B="PACKAGE B INCLUDES THAT N THIS";
var DT_C="PACKAGE C INCLUDES THOSE N THESE";
var DL_A="PACKAGE A CAN BE FOUND HERE";
var DL_B="PACKAGE B CAN BE FOUND HERE";
var DL_C="PACKAGE C CAN BE FOUND HERE";

- Then I wanna make an option box that allows you to choose A, B, or C

- And 2 buttons next to the drop-down-menu that say [details] and [download]

- When I click the details button I want to pop an alert with the appropriate DT_ message

- When I click the download button I want to pop an alert with the appropriate DL_ message

IE:
packX=getElementByID('select1');
alert("DT_"+packX);

- Now I already can get what I want done the long simple way (using if commands) but I want to learn a cleaner way...

1) I DO NOT want to use more than 1 or 2 'if' commands meaning I don't want to do something like tell the [detail] button 'if the selected value in the drop down menu is A, then-> alert(DT_A)'

2) I would rather have 1 alert(); line in the command and have it use a variable, and to have that variable set to the proper value prior to the alert.

3) I want to set the value to each of the message variables seperately to organize and make it easier to add more...

-- You don't have to make it similar to my idea at all since my idea didn't work --

BTW: In my example it may not seem like I'm trying to do the same thing mentioned above but maybe you can see where I'm going wrong

<html>
<head>
<script language="JavaScript" type="text/JavaScript">
<!--
function DET(){
var DT_A="IT WORKS!!!";
var packx=window.document.form1.pack1.value;
var desc='DT_'+packx
alert(desc);
}
//-->
</script>
</head>
<body>
<div align="center">
<form name="form1">
<div align="left">
<select name="pack1">
<option selected>-- Select Package --</option>
<option value="A">Package A</option>
<option value="B">Package B</option>
</select>
<input name="DT" type="button" id="DT" onClick="DET();" value="Details">
<input name="DL" type="button" id="DL" onClick="DET();" value="Download">
</div>
</form>
</div>
</body>
</html>

PSLoh
08-19-2005, 01:44 AM
try using eval()

BigMoosie
08-19-2005, 04:56 AM
Do not use eval, it is not a good programming practice, instead, become familiar with javaScript's object handling:

VarA="Goodness gracious!";
VarB="Var";
VarC="A";
VarD=VarB+VarC;

alert(window[VarD]);

spitrhyma
08-19-2005, 05:17 AM
-- PROBLEM SOLVED --

Holy sh!t big moos that's EXACTLY what I need! You are AWESOME!

What does Window[x] do?

I mean it isn't actually made to take the value of a variable n use it as if it's a variable's name is it? What other uses does it have?

-- PROBLEM SOLVED --

BigMoosie
08-19-2005, 06:07 AM
Basically, window is the global object in javaScript. You have probably seen objects usually written like:

myObject.myProperty;

Well, in javaScript a global variable is actually a propert of the global object (window), for example run this script:

var a="hello";
alert(window.a);

A property can also be referenced by a string, for the first example can be expressed as:

myObject["myProperty"];

Therefor it is also possible to access properties of the window object this way, hope this clears things up.