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!

Quick 'n' Simple Javascript Select Question 1

Status
Not open for further replies.

FateFirst

Programmer
Apr 15, 2002
212
GB
I have a little form which is used for searching on on my ASP page. My question is regarding the <select>. It uses javascript to chuck the data into it.

Quick Example:

Code:
var country_select = document.searchForm.COUNTRIES_VALUE;
country_select.options[0] = new Option("All");
country_select.options[0].value = 0;
country_select.options[1] = new Option("America");
country_select.options[1].value = 2;
country_select.options[2] = new Option("Austria");
country_select.options[2].value = 27;
country_select.options[3] = new Option("Bulgaria");

Now if I submit this form I want to retain the item that has been selected in the drop down but the data sent through is the value and not the selectedIndex. Is it still possible to set the selection option using just the value?

Ideally I would recode it to be an ASP generated drop but I'm just taking what was handed to me im just trying to fix it for now.

Any help would be appreciated.

Thanks!

- FateFirst
 
Will the data for the select box always be the same on the submitted page? You could loop through the options testing their value and when you find a match set the selection.

Another approach would be to execute a bit of javascript before allowing submission that takes the current selected index value and stores it into a hidden form field. Then on your receiving page you will know what the selected index value was to set the appropriate value in the select box.


At my age I still learn something new every day, but I forget two others.
 
heh dunno why I didnt think of such a simple solution (2nd para suggestion).

ty!

- FateFirst
 
Your probably still thinking on the ASP side of things.
:)

At my age I still learn something new every day, but I forget two others.
 
a much more elegant approach would be to generate the entire select box server-side. you could then easily test the value against the values you're looping through, and set the selected value (if applicable) to be selected again.



*cLFlaVA
----------------------------
[tt]mr. pibb + red vines = crazy delicious![/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
myself said:
a much more elegant approach...

not to belittle theniteowl's solution, it's just that your code won't even work if a user has javascript disabled. it's always best to handle page content on the server, rather than on the client.



*cLFlaVA
----------------------------
[tt]mr. pibb + red vines = crazy delicious![/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Yea, im aware its not great to be completely in javascript. As I said in the first post:

'Ideally I would recode it to be an ASP generated...'

This isnt my decision though and I'm just doing whats been requested.

Thanks for all the info though.

- FateFirst
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top