PDA

View Full Version : Really Basic Syntax Question



Shard
April 14th, 2007, 06:45 PM
Hi all :-)
Here's a really simple question that I can't believe has be stumped - lol
I need to reference a form element using two variable names to represent the actual form element names and the syntax of that request is driving me insane. Imagine we have a form called "theform" which has a select list called "cities" and the 3rd option is "New York". If I have stored the index number of "New York" (2) in a variable called "lookingat", and the name of the select box "Cities" in another variable called "lookingwhere", then what would the request syntax be?

document.theform.lookingwhere.options[lookingat].selected = true;

The problem is the lookingwhere inclusion in this statement, it automatically tries to find a form element called "lookingwhere" cause the syntax is wrong. I've tried surrounding it with brackets, quotes, using an eval and then splitting the reuest etc but to no avail.

I am sure its a REALLY simple matter of an extra dot or comma etc. PLEASE HELP.

Shard
April 14th, 2007, 07:55 PM
The answer was to use the dreaded eval statement, but to include in its scope the entire line including the left and right hand sides of the query. I'd paste the code in here but it crashed FF twice when I tried - obviously a gap in the html form submit security here guys - lol

Icy Penguin
April 14th, 2007, 10:53 PM
I guess you could just give that option an Id name and do this:



var blah = document.getElementById('lookingAt');

ratherblue
April 16th, 2007, 11:17 AM
document.getElementById is what you really want to use.

But if you are set on using your way.. then the syntax would be like this:


document.forms['theform'].lookingwhere['lookingat'].selected

or


document.theform.lookingwhere['lookingat'].selected


i think..