Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hide A Select Option?

Status
Not open for further replies.

scripter73

Programmer
Apr 18, 2001
421
US
Hi,

I hope my goal doesn't confuse you.

I have a select list where a user can select years.
I have a button that when clicked populates the select list with a default year "2099". I want "2099" to show in the select list, but it will not as long as it is not an option for the user.

My question. How can I get it to display without having it as an option that the user can pick? The year 2099 is not valid, and I only use it as a dummy year indicator for myself when I receive the data.

Is there a way to "hide" this option from the users?

I know this is a little off-beat, but if someone could help me out, I'd really appreciate it.

Below is a snippet of the code. If you copy it and look in your browser, you'll see what I mean. When you click on the button, it is supposed to populate the select box with 2099, but doesn't because 2099 is not an option.


<html>
<head>
<title>Add Option</title>
</head>

<body>
<form name=&quot;form1&quot;>
<select name=&quot;year&quot;>
<option value=&quot;Year&quot; selected>Year</option>
<option value=&quot;2000&quot;>2000</option>
<option value=&quot;2001&quot;>2001</option>
<option value=&quot;2099&quot;></option>
</select>
<input type=&quot;button&quot; name=&quot;select_choose&quot; onclick=&quot;document.form1.year.value='2099'&quot; value=&quot;Choose A Year&quot;>



</form>


</body>
</html>




Thanks in advance,
scripter73
 
you have to check the option value with onSubmit..lemme know if you need code.. -Greg :-Q

flaga.gif
 
<option>2099</option>

If you are checking for populated form fields on submit it will throw this back telling them to fill in this field. Note no value associated DeZiner
Never be afraid to try something new.
Remember that amateurs built the Ark.
Professionals built the Titanic
 
Load the following page and see if this is more close to home. Study the JavaScript functions so you can understand how it works.

#1: When you load the page into your browser, notice that the select list does not have the 2099 option in it.

#2: Press the button and the 2099 option is added.

#3: You can press the button again, but it will only add it once. (JavaScript is checking the last option in the list before adding. Can you see that ??)

#4: Now, after 2099 is populated into the list, try to select it... &quot;No Can Do&quot;..

If anything, it should give you a few tips on some JavaScript methods available for you.


<html>
<head>
<title>Add Option</title>
<script Language=JavaScript>
<!--
function addOpt(form_ref) {
if (form_ref.year.options[form_ref.year.length -1].value != &quot;2099&quot;) {
form_ref.year.options[form_ref.year.length] = new Option(&quot;2099&quot;,&quot;2099&quot;)
}
}
function noSelect(form_ref) {
if (form_ref.year.options[form_ref.year.selectedIndex].value == &quot;2099&quot;) {
form_ref.year.options[0].selected = true
}
}

//-->
</script>
</head>

<body>
<form name=&quot;form1&quot;>
<select name=&quot;year&quot; onChange=&quot;noSelect(this.form);&quot;>
<option value=&quot;Year&quot; selected>Year</option>
<option value=&quot;2000&quot;>2000</option>
<option value=&quot;2001&quot;>2001</option>
</select>
<input type=&quot;button&quot; name=&quot;select_choose&quot; onclick=&quot;addOpt(this.form);&quot; value=&quot;Choose A Year&quot;>



</form>


</body>
</html>


TW
 
Thanks, ToddWW,

That's very inventive how you added the option 2099.

I'll study this and work it into my main program.

Thanks a bunch!

scripter73
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top