469,917 Members | 1,773 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,917 developers. It's quick & easy.

Form is set by me

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
Jul 23 '05 #1
4 966
Scott wrote:
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
Jul 23 '05 #2
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
Jul 23 '05 #3
> 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
Jul 23 '05 #4
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[i]) 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
}
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by John | last post: by
5 posts views Thread by Richard Cornford | last post: by
4 posts views Thread by Targa | last post: by
11 posts views Thread by Jozef | last post: by
6 posts views Thread by Gary Miller | last post: by
4 posts views Thread by Alex Sibilev | last post: by
5 posts views Thread by ortaias | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.