473,320 Members | 1,950 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Set select box option to be selected based upon a variable.

8
I need to set an option from a select box to be selected based upon a value I am pulling from a database.

I cannot alter the code of the select box in this case, so I must use javascript to change it after the select box has loaded. There are several pages, some with several select boxes, that need this function. I would like one javascript function that will take take two variables (the form element and a value) and then set the correct option to be selected.

Here is an example of the code format for these select boxes. I can add id or value attributes to the options but this would be a very time consuming process as there are a large number that would need to be edited.

[HTML]
<select name="userans0" id="userans0">
<option></option>
<option>CR - Copy Recorded</option>
<option>CU - Copy Unrecorded</option>
<option>OU - Original unrecorded</option>
<option>OR - Original recorded</option>
<option>MI - Missing</option>
</select>
[/HTML]

Here is the javascript function I am trying to use, this is in the document header.

Expand|Select|Wrap|Line Numbers
  1. function selectValue(formElement, val)
  2.         {
  3.             for(i=0;i<formElement.length;i++)
  4.             {
  5.                 if(formElement.options[i].value==val)
  6.                 {
  7.                     formElement.selectedIndex=i
  8.                 }
  9.  
  10.             }    
  11.         }
  12.  
In this case the variables are the following:

formElement = document.testform.userans0
val = CU - Copy Unrecorded

And the code to call the function.
[HTML]
<script type="text/javascript" language="JavaScript">
selectValueA(formElement, val);
</script>
[/HTML]

I can't seem to get this to work.
Jan 17 '08 #1
5 2752
acoder
16,027 Expert Mod 8TB
Use the "text" property instead since you haven't set the value attribute.
Jan 18 '08 #2
kodt
8
Use the "text" property instead since you haven't set the value attribute.
Hmm, that did not seem to work either.

I also tried using value after adding the value attributes to the options.

[HTML]<option value="CR - Copy Recorded">CR - Copy Recorded</option>[/HTML]

That did not work either.
Jan 18 '08 #3
kodt
8
I have also tried this method:

If I set IDs on the options like so:

[html]<option id="CR - Copy Recorded">CR - Copy Recorded</option>[/html]

This works:

Expand|Select|Wrap|Line Numbers
  1. function setSelectValue()
  2. {
  3. var curOption = document.getElementById("CR - Copy Recorded");
  4. curOption.selected = true;
  5. }
  6.  
But this does not:

Expand|Select|Wrap|Line Numbers
  1. function setSelectValue(val)
  2. {
  3. var curOption = document.getElementById(val);
  4. curOption.selected = true;
  5. }
  6.  
Jan 18 '08 #4
kodt
8
I found a stupid mistake with my input, the last function I wrote works correctly although I will need to add id attributes to several pages. If anyone has additional input to get the original function to work that would be appreciated.
Jan 18 '08 #5
acoder
16,027 Expert Mod 8TB
If you use the text property, it does work. Perhaps you're calling the wrong function, selectValueA instead of selectValue?
Jan 18 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Guy Hocking | last post by:
Hi there, I have a problem in my ASP/SQL Server application i am developing, i hope you guys can help. I have a ASP form with list boxes populated by SQL tables. When a user selects a value...
3
by: Shawn Modersohn | last post by:
I've got <form name="form1> <select name="select1"> <option value="null" selected>Select A Team</option> <option value="Cougars">Cougars</option> <option value="Mavericks">Mavericks</option>...
6
by: Ben Hallert | last post by:
Hi guys, I'm trying to figure out what bone headed mistake I made on something I put together. I've got a form (named 'context') that has a variable number of select-multiple inputs on it. ...
6
by: passion_to_be_free | last post by:
This is probably simple, but I can't seem to find it anywhere. I have have some values stored in javascript variables. I have a <select> dropdown list whose options correspond to these values. I...
5
by: callmebill | last post by:
I'm relatively new to javascript, and I'm trying to decide whether the following (and if so, clues on how to do it): I'd like to create two HTML multiple-select boxes. The first would be a list...
9
by: Susan Bricker | last post by:
Hi. I have two questions ... (1) I want to use a Listbox to enable the user to select 1 or many items from the list. However, I'm having trouble figuring out how to find out t which items have...
4
by: Matt Ratliff | last post by:
Hello, I would appreciate any assistance you have with the following problem: I have (as an example) an array of values as follows: arrayvalues=new Array("0001","0003","0005") where each is the...
2
by: rickl2790 | last post by:
Hi All I am putting a form together and I need to be able to hide or display elements of the form based on what is selected with a select. The form is a passenger details entry form and the...
2
by: Sudhakar | last post by:
i have two select tags as part of a registration form, city1 city2 where city1 has a list of regions and similar for city2 there are different regions for city1 and city2 so instead of all the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.