Form is set by me

S

Scottie

I have a drop-down box. I also have a var with a value. I want the
value in the var to be displayed in the drop-down box. How can I do
this?

Thanks.

Scott
 
B

Bart Van der Donck

Scott said:
I have a drop-down box. I also have a var with a value. I want the
value in the var to be displayed in the drop-down box. How can I do
this?

How should your var behave in regard to your dropdownbox? Is your
dropdownbox already populated with <option>'s, before the code is
invoked that would alter its content? Should the dropdownbox react on
events on the webpage to get new content?

Reading your question as it is stated, this basic code could be an
answer:

var myvar = "somevalue";
document.write('<select size=1 name=myselectbox>');
document.write('<option value="'+myvar+'">'+myvar+'</option>');
document.write('</select>');

Bart
 
S

Scottie

Bart,
How should your var behave in regard to your dropdownbox? Is your
dropdownbox already populated with <option>'s, before the code is
invoked that would alter its content? Should the dropdownbox react on
events on the webpage to get new content?

I didn't get my message clear enough. My var already has a value. My
dropdownbox is already populated with <options>'s. What I want to know
is can my var be matched with the <option>'s directly and the <option>
get executed.

Scott
 
B

Bart Van der Donck

I didn't get my message clear enough. My var already has a value. My
dropdownbox is already populated with <options>'s. What I want to know
is can my var be matched with the <option>'s directly and the <option>
get executed.

Well you still left a few things unspecified, but guessing at those,
this code should do the trick:

<html>
<head>
<!-- var already has value, I'm setting it here -->
<script language="javascript">
var myvar = "somevalue";
</script>
</head>
<body>
<!-- your dropdownbox with its options, set in html -->
<form action="somescript.cgi">
<select size="1" name="mybox">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</form>
<!-- now change contents of the box -->
<script language="javascript">
// empty the box
while (document.forms[0].mybox.options.length)
document.forms[0].mybox.options[0] = null;
// put var in the box
document.forms[0].mybox.options[document.forms[0].mybox.length]
= new Option(myvar, myvar);
// now set which option must be selected.
// required in NS4, and handy in IE when you want
// to select an option that doesn't come first
document.forms[0].mybox.selectedIndex = 0;
// now "execute" the dropdownbox: do you mean submitting the form?
// if so, uncomment this line:
// document.forms[0].submit();
</script>
</body>
</html>

Bart
 
S

Scottie

Bart,

I came up with this. It works:

var str = window.location.search; // returns
'?text=language' from the URL
if (str != "") {
var temp = str.split('=');
for (i=1; i <= speclang.length; i++) { // goes through whole
language array
if (temp[1] == speclang) break; // found
}
if (i > speclang.length) { // didn't find
alert(temp[1] + " was not found in the Mexican language list.");
return;
}
}
document.forms['Language'].selectname.selectedIndex = i; // make
dropdown box = passed language
languag(i); // update to specific language
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top