Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Form is set by me

Reply
Thread Tools

Form is set by me

 
 
Scottie
Guest
Posts: n/a
 
      05-23-2004
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
 
Reply With Quote
 
 
 
 
Bart Van der Donck
Guest
Posts: n/a
 
      05-23-2004
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
 
Reply With Quote
 
 
 
 
Scottie
Guest
Posts: n/a
 
      05-23-2004
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
 
Reply With Quote
 
Bart Van der Donck
Guest
Posts: n/a
 
      05-24-2004
> 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
 
Reply With Quote
 
Scottie
Guest
Posts: n/a
 
      05-24-2004
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
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to retrieve form field value if form is EncType=multipart/form-dataForm? Li Zhang ASP .Net 4 02-27-2009 01:23 AM
<form>...</form> - how to supress blank space after </form> in IE? rob c Javascript 4 12-30-2005 01:18 PM
Treeview questions - how to set set start node and how to catch click event Alan Silver ASP .Net 0 12-21-2005 10:40 AM
submit form, validate form, set cookie, send email, download file mhawkins19@adelphia.net Javascript 1 03-17-2005 08:19 AM
Unable to set focus to textfield in a applet if browser is set to Sun JRE 1.4 Manav Java 0 10-15-2003 03:42 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57