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

Glitch in dynamic drop down 1

Status
Not open for further replies.

frozenpeas

Technical User
Sep 13, 2001
893
CA
Hello,

I searched the forum and FAQ section for help on this but came up empty.

Each select item in my dynamically generated drop down is followed by a blank one. For example it looks like:
Code:
Please select...
(option 1)
(        )
(option 2)
(        )
(option 3)
(        )
(option 4)

I cannot determine what is causing this. My menu generating code is currently:
Code:
pairCount = results.length/2;
	document.entries_form.subcat_id.length = pairCount;
	for (i=0; i<results.length; i++){
		if((i%2)==0){
			//even
			document.entries_form.subcat_id[i].text = results[i];
			document.entries_form.subcat_id[i].value = results[i+1];
		}else{
			//odd
			document.entries_form.subcat_id[i+1].text = results[i+1];
			document.entries_form.subcat_id[i+1].value = results[i+2];
		}
	}

the output I am assigning to the array looks good:
Code:
Bed & Breakfast|1|Boutique Hotel|2|Conference Centre|3|Furnished Apartments|4|Hotel - Large Scale|5|Hotel - Mid-Sized|6|Other|8|Staff Accommodations|7|

I have also tried generating the menu with this loop but have the same result:

Code:
document.entries_form.subcat_id.length = results.length/2;
	for (i=0; i<results.length; i+=2){
		document.entries_form.subcat_id[i].text = results[i];
		document.entries_form.subcat_id[i].value = results[i+1];
	}

Any ideas as to what is causing the blank selects?

Thanks.

frozenpeas
--
Micfo.com Affiliate Program
 
The "i+=2" and the "subcat_id[i+1]" is what's doing it. In both cases, you're skipping a position when assigning to the select element. Don't do thism and you'll be set.

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
I don't understand. Since my even array values are storing the names and the odd values are storing the values, I need to be assigning the select text and value as such, right? So that is why I have 'i' for the name and 'i+1' for the value. Then the even/odd bit is so the values are assigned in proper pairs.

Am I way off in my thinking here?

frozenpeas
--
Micfo.com Affiliate Program
 
No - buy you are confusing your source array, and the destination options "array".

The source options do need the +1, but the destination options do not.

Perhaps having two counters would be the simplest way for you to achieve this.

Hope this helps,
Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
So basically:

Code:
var selObj = document.forms['entries_form'].elements['subcat_id'];
selObj.length = results.length/2;
for (i=0, j=0; i<results.length; i+=2, j++){
    selObj[j].text = results[i];
    selObj[j].value = results[i+1];
}

Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
You can do it with one counter, try making this change:
Code:
document.entries_form.subcat_id.length = results.length/2;
    for (i=0; i<results.length; i[!]++[/!]){
        document.entries_form.subcat_id[i].text = results[[!]2 * i[/!]];
        document.entries_form.subcat_id[i].value = results[[!](2 * i) + 1[/!]];
    }

-kaht

[small]How spicy would you like your chang sauce? Oh man... I have no idea what's goin' on right now...[/small]
[banghead]
 
You're welcome.

-kaht

[small]How spicy would you like your chang sauce? Oh man... I have no idea what's goin' on right now...[/small]
[banghead]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top