Click to See Complete Forum and Search --> : JavaScript Variables - newbie


nbrunskill
12-17-2002, 02:50 PM
Hi,

I'm rather new to JavaScript and have a question. I am creating a conditional value list. In the script below I have the the bracketed value for the arrays written as text (such as "Smallville_Ep_1" for the first array). In the html form the options match correctly, but the conditinal list does not change as new items are selected. If I change the code to something like "aLBValues[1] = new Array" it seems to work. So, I guess my question is do these values have to be numbers? I apologize if this sounds confusing, I'm not familiar with JavaScript. The script I'm working with is an example I have modified. If this sounds confusing I can post the entire html doc, but the script in the header is posted below.
thanks!
Nate




<SCRIPT langauge=javascript>

var aLBValues = new Array() ;

aLBValues[Smallville_Ep_1] = new Array("FOG2", "FOG3", "XRAY2", "XRAY2", "BYRON2", "BYRON2", "THROW4", "COPTER1", "COPTER2", "RIG1", "SPEED4", "SPEED6", "SPEED6", "SPEED5", "");

aLBValues[Gabrielle] = new Array("SIGN1", "THROW3", "");

aLBValues[Overhead] = new Array("");

aLBValues[System_Admin] = new Array("");

aLBValues[Smallville_Ep_2] = new Array("THROW1", "THROW3", "THROW4", "COPTER1", "COPTER3", "COPTER3", "TILT1", "SPEED2", "SPEED4", "SPEED3", "SPEED5", "BYRON3", "");

aLBValues[Smallville_Ep_3] = new Array("FOG1", "BYRON2", "THROW1", "THROW5", "THROW5", "SPEED1", "TILT1", "SPEED2", "RIG1", "BYRON4", "");

aLBValues[Smallville_Ep_4] = new Array("SPEED1", "COPTER3", "SPEED3", "BYRON4", "");

aLBValues[Devil's_Throat] = new Array("", "FOG1", "FOG2", "FOG3", "FOG4", "FOG4", "COPTER1", "COPTER3", "RIG1", "WELL1", "WELL1", "BYRON3", "BYRON3", "BYRON4", "");

aLBValues[Smallville_Ep_5] = new Array("");



function addLBElement(oLB, value, text)
{
var oNewOption = new Option(text) ;
oNewOption.value = value;

oLB.options[oLB.options.length] = oNewOption ;
}

function updateLB()
{
var oLBSource = document.the_form.timeCardID::t_show ;
var oLBDest = document.the_form.timeCardID::t_shotName ;
var nSelId = oLBSource[oLBSource.selectedIndex].value ;

oLBDest.length = 0 ; // Clear the LB

for(nLoop = 0; nLoop < aLBValues[nSelId].length; nLoop++)
{
addLBElement(oLBDest, nLoop, aLBValues[nSelId][nLoop]) ;
}
}
function onLoad()
{
updateLB() ;
}
</SCRIPT>

gil davis
12-17-2002, 04:47 PM
the bracketed value for the arrays written as text (such as "Smallville_Ep_1" for the first array)...
aLBValues[Smallville_Ep_1] = ...
Without quotes around the text in the brackets, it becomes a variable. If the variable is not equated anywhere, then the variable contains null. Null is not a legal array index.

nbrunskill
12-18-2002, 05:33 PM
If I give the variables a definition of any kind (text or numeric) it doesn't seem to work. If I place a 1 in the brackets without quotes, it seems to work. Does it just see that as a numeric value and not a variable since it is a digit? Can I use anything other than digits in this space (between the brackets?).
thanks!
Nate

gil davis
12-18-2002, 06:37 PM
I just noticed this:
<SCRIPT langauge=javascript>
You spelled language wrong.
Originally posted by nbrunskill
If I give the variables a definition of any kind (text or numeric) it doesn't seem to work.
It ought to. I have an array in one of my pages that I load like this:

var i = 0;
var m = new Array();
m = "some text";
... [i]ad nauseum ...

If I place a 1 in the brackets without quotes, it seems to work. Does it just see that as a numeric value and not a variable since it is a digit?
Yes. Numbers not delimited by quotes are numeric values.
Can I use anything other than digits in this space (between the brackets?).
Yes. Array indexes can be a valid constant. The images array in the document is a perfect example. If you create an image in HTML:

<img src="..." name="image1">

it becomes part of the document.images array. You can access it either using:

document.images["image1"]

or (assuming it is the first or only image):

document.images[0]

nbrunskill
12-18-2002, 07:24 PM
Hi Gil,

Thanks for the info. I have one more question if it's not too much trouble. In a form name and JS statement am I allowed to have colons, do they confuse the statement? I ask because what I am trying to do is use JS and CDML to have form info submitted to a FileMaker database. The "timeCardID::t_shotName" you see in the JS and HTML exmaples below are what send the form info to the db. It's the CDML. Are these colons confusing the hell out of the htmla and JS? If so, do you happen to know of any work arounds for a situation like this?
sorry for all the questions
Nate





var oLBDest = document.the_form.timeCardID::t_shotName ;



<select name="timeCardID::t_shotName">
<option>DUMMY VALUES
<option>DUMMY VALUES
<option>DUMMY VALUES
</select>

nbrunskill
12-18-2002, 08:08 PM
Well, I found that the ":" is used with conditional operators, so I assume it would be fair to say that I should not use them in my variables and form names. Nuts. Is there anyone who uses a CDML portal with JavaScript submission?

nbrunskill
12-20-2002, 04:15 PM
Hi again,

Other than the FileMaker portal issue with the colon, the other problem I've been having with my script (code in an above post) is that it submits the number for the conditional list box item, rather than the text. For example, using the array:

aLBValues[Gabrielle] = new Array("SIGN1", "THROW3", "");

"Gabrielle" will submit "Gabrielle to the database" but "SIGN1" will submit "0" and "THROW3" will submit "1". Does anyone have any ideas as to how I can ameliorate this?
thanks very much!
Nate

gil davis
12-20-2002, 04:29 PM
JS 1.2 was the first version to allow creating arrays as you are doing. Remember when I pointed out that you spelled "language" wrong?

<script type="text/javascript" language="JavaScript1.2">

Not sure if it will make a difference. IE will pretty much ignore it anyway, but who knows?

Perhaps using the literal method will work better for you:

aLBValues["Smallville_Ep_1"] = ["FOG2", "FOG3", "XRAY2", "XRAY2", "BYRON2", "BYRON2", "THROW4", "COPTER1", "COPTER2", "RIG1", "SPEED4", "SPEED6", "SPEED6", "SPEED5", ""];

nbrunskill
12-20-2002, 07:03 PM
Hi Gil,

I went ahead and changed the

<script type="text/javascript" language="JavaScript1.2">

tag It didn't seem to change anything, but it's good for safe keeping anyway.

And, I must confess I'm clear on what you mean by the "literal method". I not sure but I think I need to define the value of the select box items and I'm not sure how to go about that. I could be wrong, but the reason I suspect this is because what the script does is replace the values of the form below (the "DUMMY VALUES") with the selected array. I guess it must do so without defining any values for the options, so it submits the the array index number to the database instead.
Does this sound like it could be the case?

<select name="shot1">
<option>DUMMY VALUES
<option>DUMMY VALUES
<option>DUMMY VALUES
</select>

gil davis
12-20-2002, 09:21 PM
changed the tag
If nothing else, it will be forward compatible, and IE doesn't care anyway.
I'm clear on what you mean by the "literal method"
That method creates an array without using the Array() object, that's all.
<option>DUMMY VALUES
After you change the array, dump it out and see what is in it.

for (var i in oLB.options) {
alert(i + ": " + oLB.options[i].value + " = " + oLB.options[i].text);}