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

Building dynamic select list - can I specify selected option? 1

Status
Not open for further replies.

LTeeple

Programmer
Aug 21, 2002
362
CA
Hi all,
My code loops through an array, assigning each value as an option to one select list. I always want "Choose time: " to appear in the select list, so before I enter the loop, I start with that option (index of 0).

From there, I start adding options to the select list:
var = new Option(value,index);

Is there a way in my code that I could ensure that any one of the values I add in will be selected when the select list is finished drawing?

I looked up options.selected but that seems to be used for retrieving values. option.selectedIndex seems to be used only for retrieving value of selected Option.

Any help is greatly appreciated.

[cheers]
Cheers!
Laura
 
How about try this?
[tt]
oselect.options[oselect.length-1].selected="selected";
[/tt]
where oselect is the select element's reference. The index being oselect.length-1 because I assume here the new option being just created and is added as the last index --- otherwise, an obvious adjustment.
 
Laura, you can also use the values of the pulldown options. I typed up this example that allows you to type a value into a textbox and press the button, the pulldown is then set to that value (if it exists)
Code:
<script language=javascript>
function setPulldownOption(num) {
   document.forms["blahForm"].elements["blahSelect"].value = num;
}
</script>
<body>
<form name=blahForm>
<select name=blahSelect>
<option value=1>1
<option value=2>2
<option value=3>3
<option value=4>4
<option value=5>5
</select>
<br><br><br>
<input type=text name=blahText><br>
<input type=button value='Set as selected value' onclick='setPulldownOption(document.forms["blahForm"].elements["blahText"].value)'>
</form>
</body>

-kaht

...looks like you don't have a job, so why don't you get out there and feed Tina.
headbang.gif
[rockband]
headbang.gif
 
tsuji,

Thank you, your post was most helpful in solving my problem.

[cheers]
Cheers!
Laura
 
glad I could help

-kaht

...looks like you don't have a job, so why don't you get out there and feed Tina.
headbang.gif
[rockband]
headbang.gif
 
... sorry...

kaht, hopefully you know that I am always grateful for the time you take to respond to my questions!

[cheers]
Cheers!
Laura
 
It's ok, and it's not just you.

However (and especially lately it seems) when I or other experts here take the time to type up solutions to problems that posters have they go completely abandoned. For that reason I rarely even post in threads that already have a response. And that's a shame because often times there's more that can be added to a solution, or a completely different solution(and sometimes better, not necessarily this case - tsuji definitely knows his stuff). So, many times those solutions go unposted just because they often go ignored.

-kaht

...looks like you don't have a job, so why don't you get out there and feed Tina.
headbang.gif
[rockband]
headbang.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top