/    Sign up×
Community /Pin to ProfileBookmark

JavaScript Variables – newbie

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>

to post a comment
JavaScript

9 Comments(s)

Copy linkTweet thisAlerts:
@gil_davisDec 17.2002 — the bracketed value for the arrays written as text (such as "Smallville_Ep_1" for the first array)...

aLBValues[Smallville_Ep_1] = ...[/quote]

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.
Copy linkTweet thisAlerts:
@nbrunskillauthorDec 18.2002 — 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
Copy linkTweet thisAlerts:
@gil_davisDec 19.2002 — I just noticed this:
[b]<SCRIPT langauge=javascript>[/b][/quote]
You spelled language wrong.
[i]Originally posted by nbrunskill [/i]

[B]If I give the variables a definition of any kind (text or numeric) it doesn't seem to work.[/b][/quote]

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[i++] = "some text";

... [i]ad nauseum[/i] ...

[b]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?[/b][/quote]
Yes. Numbers not delimited by quotes are numeric values.
[b]Can I use anything other than digits in this space (between the brackets?).[/b][/quote]
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:

[font=courier]<img src="..." name="image1">[/font]

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

[font=courier]document.images["image1"][/font]

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

[font=courier]document.images[0][/font]
Copy linkTweet thisAlerts:
@nbrunskillauthorDec 19.2002 — 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>
Copy linkTweet thisAlerts:
@nbrunskillauthorDec 19.2002 — 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?
Copy linkTweet thisAlerts:
@nbrunskillauthorDec 20.2002 — 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
Copy linkTweet thisAlerts:
@gil_davisDec 20.2002 — 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?

[font=courier]<script type="text/javascript" language="JavaScript1.2">[/font]

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:

[font=courier]aLBValues["Smallville_Ep_1"] = ["FOG2", "FOG3", "XRAY2", "XRAY2", "BYRON2", "BYRON2", "THROW4", "COPTER1", "COPTER2", "RIG1", "SPEED4", "SPEED6", "SPEED6", "SPEED5", ""];[/font]
Copy linkTweet thisAlerts:
@nbrunskillauthorDec 21.2002 — 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>
Copy linkTweet thisAlerts:
@gil_davisDec 21.2002 — changed the tag[/quote]
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"[/quote]
That method creates an array without using the Array() object, that's all.
<option>DUMMY VALUES[/quote]
After you change the array, dump it out and see what is in it.

[font=courier]for (var i in oLB.options) {

alert(i + ": " + oLB.options[i].value + " = " + oLB.options[i].text);}[/font]
×

Success!

Help @nbrunskill spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...