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!

dynamic menus

Status
Not open for further replies.

lucidtech

IS-IT--Management
Joined
Jan 17, 2005
Messages
267
Location
US
I'm creating drop down menus dynamically from a query in coldfusion. There will be from 1 to 10 drop down menus available depending on the data... if there is only data for 2 there will only be 2 shown in the form.

My problem is I'm jusing javascript to calculate product prices depending on what is selected in each drop down. Is there a way I can hide the drop downs that aren't populated with data, or for java to check whether there is data in them or not. Here is what I have -

if (document.product_info.product_option2_value.options[document.product_info.product_option2_value.selectedIndex].value != null) {
option2 = 0;
} else {
option2 = document.product_info.product_option2_value.options[document.product_info.product_option2_value.selectedIndex].value * sq_footage;
}
if (document.product_info.product_option3_value.options[document.product_info.product_option3_value.selectedIndex].value != null) {
option3 = 0;
} else {
option3 = document.product_info.product_option3_value.options[document.product_info.product_option3_value.selectedIndex].value * sq_footage;
}

only thing is that if there isn't a drop down with that name, the fucntion doesn't work at all. I've tried this and tried using the isNaN function.. still nothing
 
Give this a go. It assumes:
- The select elements are numbered 1-10 not 0-9...
- The variables 'option1', 'option2', ... 'option10' are global variables

Code:
var frm = document.forms['product_info'].elements;
for (var loop=1; loop<11; loop++) {
	var selObj = frm['product_option' + loop + '_value'];
	if (selObj) {
		window['option' + loop] = selObj.options[selObj.selectedIndex].value * sq_footage;
	} else {
		window['option' + loop] = 0;
	}
}

Hope this helps,
Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Sorry Dan... I've been MIA for a while due to internet connection problems... I got it to work using this code :

for ( i=1; i<10; i++ ) {
if(eval("document.product_info.product_option"+i+"_value.value==\"none\"")) {
eval("option"+i+"=0");
} else {
eval("option"+i+"_base=Number(document.product_info.product_option"+i+"_value.options[document.product_info.product_option"+i+"_value.selectedIndex].title)");
eval("option"+i+"_sqft=Number(document.product_info.product_option"+i+"_value.options[document.product_info.product_option"+i+"_value.selectedIndex].value)");
eval("option"+i+"_sqft_total=Number(option"+i+"_sqft)*Number(sq_footage)");
eval("document.product_info.product_option"+i+"_title.value=document.product_info.product_option"+i+"_value.options[document.product_info.product_option"+i+"_value.selectedIndex].label");
eval("base_price=Number(option"+i+"_base)+Number(base_price)+Number(option"+i+"_sqft_total)");
}
}
 
Oh dear... my pet hate (over- and mis-using eval)... and here it is in spades! :-)

Try this as a hastily-coded and not-at-all-tested replacement:

Code:
var frm = document.forms['product_info'].elements;
for (var i=1; i<10; i++ ) {

	prodVal = frm['product_option' + i + '_value'].value;

	if (prodSel.value == 'none') {
		window['option' + i] = 0;
	} else {
		window['option' + i + '_base'] = Number(prodSel.options[prodSel.selectedIndex].title);
		window['option' + i + '_sqft'] = Number(prodSel.options[prodSel.selectedIndex].value);
		window['option' + i + '_sqft_total'] = Number(window['option' + i + '_sqft']) * Number(sq_footage);
		frm['product_option' + i + '_title'].value = prodSel.options[prodSel.selectedIndex].label;
		base_price = Number(window['option' + i + '_base']) + Number(base_price) + Number(window['option' + i + '_sqft_total']);
	}
}

You can see the use of the "['xx' + y + 'zz']" syntax to access form fields (and indeed, global variables) without the use of eval - which should give you a significant speed increase.

Hope this helps,
Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top