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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Update data on DropDown List

Status
Not open for further replies.

QueenP

Programmer
Oct 22, 2002
16
BR
I'd like to know if there's a way to update the data on a dropdown list.

I was thinking about something that works like the not-in-list event from Access.

Thanks.
 
when do you want the data updated?

If while the page is already loaded, then all possible values must be transmitted to the page and then you would use javascript to change the values.

Like the drop downs on this page?:

And I do not use Access so can not help you with the second part of the question.

Kris
- There's never time to do it right, but there's always time to do it over.
 
It's not exactly like this.

What I was looking for is if the information I want is not in the dropdown, then I add it.
 

you would most likely have to have a seperate textbox to add new items. and then repost the form to save the data and requry the inforamtion to fill in the combobox.

Kris
 
ACtually this can be done server-side with a text input and a button, to add the value entered in the text input as a new option in the select box and then make it the selected option:
Code:
<html>
<head>
<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
<!--
function addNewOption(txtName,selName){
	var optionText;
	eval(&quot;optionText = yourform.&quot;+txtName+&quot;.value&quot;);

	var elem;
	eval(&quot;elem = yourform.&quot;+selName);
	elem.options[elem.options.length] = new Option(optionText,optionText);
	elem.selectedIndex = elem.options.length - 1;
	eval(&quot;yourform.&quot;+txtName+&quot;.value = '';&quot;);
}
//-->
</SCRIPT>
</head>
<body>
<form method=POST action=&quot;yourpage.asp&quot; name=&quot;yourform&quot;>
<select name=&quot;selColor&quot;>
	<option value=&quot;&quot;> [Select Color] </option>
	<option value=&quot;red&quot;> Red </option>
	<option value=&quot;blue&quot;> Blue </option>
	<option value=&quot;green&quot;> Green </option>
</select>
<input type=&quot;button&quot; value=&quot;<<&quot; onClick=&quot;addNewOption('txtNewColor','selColor');&quot;>
<input type=&quot;text&quot; name=&quot;txtNewColor&quot;>
</form>
</body>
</html>

Basically what this is doing is when the function is called you pass it the name of your input and the name of the select box you wish to add the value to. Then it creates a new option object in the options collection for the select box, assigning it to the bottom of the list. Since the index for the options starts at 0 we can assign the new object to the count of the option (elem.options.length) without being worried that it may already have a value, therefore this will work just as well if you are generating your options from ASP. Be forewarned, I just wrote this so it may have adverse side effects in some browsers, I meant it mainly just as an example.

-Tarwn --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
For my next trick I will pull a hat out of a rabbit (if you think thats bad you should see how the pigeon feels...) :p
 
Thanks Tarwn,

This is exactly what I was looking for.
 
Correction to my previous post, that first server-side should have read client-side, sorry for any confusion,
-Tarwn --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
For my next trick I will pull a hat out of a rabbit (if you think thats bad you should see how the pigeon feels...) :p
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top