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

question about options in an HTML select

Status
Not open for further replies.

Placido

MIS
Mar 13, 2002
105
IT
can you replace the value of the index in the below statement:

document.form.datab.options[0].selected

with a variable (cont)?

document.form.datab.options[cont].selected
 
Yes.

var cont = 2;
document.form.datab.options[cont].selected;

Or

var cont = 2;
document.form.datab.selectedIndex = cont;
Get the Best Answers! faq333-2924
"A witty saying proves nothing." - Voltaire
mikewolf@tst-us.com
 
I try and it doesn't work...

Here is my code:
Function datobase(){
var cont=0;
for (cont=0; cont<=12; cont++) {
if (document.form.datab.options[cont].selected==true){
if (document.form.db.value==&quot;&quot;){
document.form.db.value=document.form.datab.options[cont].value;
}else{
document.form.db.value+=&quot;,&quot;+document.form.datab.options[cont].value;
}
}
}
}
 
I recommend against using &quot;form&quot; as a name for your form...
I need to see more code...

if (document.form.datab.selectedIndex == cont){ Get the Best Answers! faq333-2924
&quot;A witty saying proves nothing.&quot; - Voltaire
mikewolf@tst-us.com
 
Here the select element:

<Select name=&quot;datab&quot; multiple size=&quot;2&quot; onblur=&quot;datobase()&quot;>
<option value=&quot;oh-adc&quot;>Ohio Administrative Code</option>
<OPTION value=&quot;oh-cs&quot;>Ohio Cases</OPTION>
<OPTION value=&quot;oh-cs-all&quot;>Ohio Case All</OPTION>
<OPTION value=&quot;oh-st-ann&quot;>Ohio Statutes Annotated</OPTION>
</Select>

That's the error I get

'document.form.datab.options[...].selected' is null or not an object...
 
That's correct. You need to do two things
[ol][li]rename your form ... <form name=&quot;myForm&quot;>
[li]Change this line if (document.form.datab.options[cont].selected==true){ to
if (document.myForm.datab.selectedIndex == cont){
[/ol] Get the Best Answers! faq333-2924
&quot;A witty saying proves nothing.&quot; - Voltaire
mikewolf@tst-us.com
 
Hi-

Thanks for your help, but what I'm trying to accomplish here is to check how many databases the user using this form selected, and send the list of databases to my host each separate by a comma in the variable &quot;db&quot;...

I think what you showed me will only accomplish to verify that the index is equal to the variable cont...

Hope this will clear your preconceptions
 
This should work for you....

<script>
function datobase(){
var cont = 0;
var dbList = &quot;&quot;;
for (cont=0; cont < document.myForm.datab.options.length; cont++) {
if (document.myForm.datab.options[cont].selected == true){
dbList += &quot;,&quot; + document.myForm.datab[cont].value
}
}
if (dbList.length > 0){
dbList = dbList.substr(1) //peel off first comma
}
document.myForm.showIt.value = dbList
}

</script>

<form name=&quot;myForm&quot;>
<Select name=&quot;datab&quot; multiple size=&quot;2&quot; onblur=&quot;datobase()&quot;>
<option value=&quot;oh-adc&quot;>Ohio Administrative Code</option>
<OPTION value=&quot;oh-cs&quot;>Ohio Cases</OPTION>
<OPTION value=&quot;oh-cs-all&quot;>Ohio Case All</OPTION>
<OPTION value=&quot;oh-st-ann&quot;>Ohio Statutes Annotated</OPTION>
</Select>
<br>
<input name=&quot;showIt&quot;>
Get the Best Answers! faq333-2924
&quot;A witty saying proves nothing.&quot; - Voltaire
mikewolf@tst-us.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top