I realise that the subject of hierachical select boxes has come up on this forum a few times in the past but I need help on an elaboration of it. I think this is quite difficult so any help greatly appreciated.
I have 2 select boxes. the box on the left dictates content of box on the right.
[URL]http://www.holdenandco.co.uk/public/selectboxproblem.html
Problem lies with setting which option will be selected when the page loads. I have succesfully used PHP to go through an array, when it finds the value in the array which matches a varaible’s value which is drawn from a sql database PHP gets the index of its array and stores that as a variable. I then just print (“var n= that PHP variable”) so to speak.
so now when the client side stuff starts we have eg var n=1 half-way down the javascript code on the link above. this is then compared to the index value of the javascript array when writing out the select box code and if the javascript array’s index value is the same as the n variable then it wirtes <option selected.. instead of just <option..
this works fine BUT… I need to do the same thing on the second drop down box. the PHP will bring back the index number of the second select boxes VALUE array index number. If they are equal (and they can only be equal if the corresponding left select box has the appropriate selection)
then I want it to again write <option selected.. instead of just <option.. for that particular <option>.
Any ideas anyone? ?
just to make clear that the php is not a problem, this is just a javascript problem, only mentioned the php for completeness.
My unsuccesful attempt, (which could be a long way from working) is here:
[URL]http://www.holdenandco.co.uk/public/selectboxproblemmyattempt.html
then I want it to again write <option selected.. instead of just <option.. for that particular <option>. [/QUOTE]
[code=php]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>dynamic select lists</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<script type="text/javascript">
//<![CDATA[
<!--
function changeMenu(idx) {
var Obj=document.MyForm.SelectSecondary;
//alert(Obj.options.length);
//delete options
for(var i=Obj.options.length; i>=0; i--) {
Obj.options[i]=null;
}
//add options
for(i=0; i<subopt[idx].length; i++) {
Obj.options[i]=new Option(subopt[idx][i], i);
}
}
var n = 0; // initilize first select
var catopt = ["Main Option 1","Main Option 2","Main Option 3"];
var subopt = [];
subopt[0] = ["Sub-option value 1","Sub-option value 2","Sub-option value 3","Sub-option value 4"];
subopt[1] = ["Sub-option value 5","Sub-option value 6"];
subopt[2] = ["Sub-option value 7","Sub-option value 8","Sub-option value 9","Sub-option value 10"];
//optionName = new Option([text, value, defaultSelected, selected])
var tbl='<FORM NAME="MyForm"><TABLE BORDER = 0 BGCOLOR = "#F8D85A" CELLPADDING = 3 CELLSPACING = 0>'+
'<TR><TD COLSPAN = 3><FORM NAME = "SelectMenu">'+
'<FONT SIZE = 4 FACE = "Times">'+
'</TD></TR><TR><TD ALIGN = LEFT>'+
'<FONT SIZE = 2 FACE = "Times" >Main Menu</FONT><BR>'+
'<SELECT NAME="SelectPrimary" onChange="changeMenu(this.options[this.selectedIndex].value);">';
for(var i=0; i<catopt.length; i++) {
if(i==n) {
tbl+='<OPTION VALUE="'+i+'" SELECTED>'+catopt[i];
}
else {
tbl+= '<OPTION VALUE="'+i+'">'+catopt[i];
}
}
tbl+='</SELECT></TD><TD><FONT SIZE = 2 FACE = "Times" >Sub- Menu</FONT><BR>'+
'<SELECT NAME="SelectSecondary" onChange="n=this.options[this.selectedIndex].value;alert("n="+n);">';
tbl+='</TD><TD VALIGN = BOTTOM></TD></TR></FORM></TABLE>';
document.write(tbl);
// Initilize "SelectSecondary"
changeMenu(n);
//-->
//]]>
</script>
</head>
<body>
</body>
</html>
[/code]
[code=php]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>dynamic select lists</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<script type="text/javascript">
//<![CDATA[
<!--
function changeMenu(idx) {
//change n
n=idx;
var Obj=document.MyForm.SelectSecondary;
//delete options
for(var i=Obj.options.length; i>=0; i--) {
Obj.options[i]=null;
}
//add options
for(i=0; i<subopt[idx].length; i++) {
if(i==n2) {
Obj.options[i]=new Option(subopt[idx][i], i, false, true);
}
else {
Obj.options[i]=new Option(subopt[idx][i], i);
}
}
}
var n = 1; // initilize first select
var n2=1; // initilize second list
var catopt = ["Main Option 1","Main Option 2","Main Option 3"];
var subopt = [];
subopt[0] = ["Sub-option value 1","Sub-option value 2","Sub-option value 3","Sub-option value 4"];
subopt[1] = ["Sub-option value 5","Sub-option value 6"];
subopt[2] = ["Sub-option value 7","Sub-option value 8","Sub-option value 9","Sub-option value 10"];
var tbl='<FORM NAME="MyForm"><TABLE BORDER = 0 BGCOLOR = "#F8D85A" CELLPADDING = 3 CELLSPACING = 0>'+
'<TR><TD COLSPAN = 3><FORM NAME = "SelectMenu">'+
'<FONT SIZE = 4 FACE = "Times">'+
'</TD></TR><TR><TD ALIGN = LEFT>'+
'<FONT SIZE = 2 FACE = "Times" >Main Menu</FONT><BR>'+
'<SELECT NAME="SelectPrimary" onChange="changeMenu(this.options[this.selectedIndex].value);">';
for(var i=0; i<catopt.length; i++) {
if(i==n) {
tbl+='<OPTION VALUE="'+i+'" SELECTED>'+catopt[i];
}
else {
tbl+= '<OPTION VALUE="'+i+'">'+catopt[i];
}
}
tbl+='</SELECT></TD><TD><FONT SIZE = 2 FACE = "Times" >Sub- Menu</FONT><BR>'+
'<SELECT NAME="SelectSecondary" onChange="n2=this.options[this.selectedIndex].value;alert("n2="+n2);">';
tbl+='</TD><TD VALIGN = BOTTOM></TD></TR></FORM></TABLE>';
document.write(tbl);
// Initilize "SelectSecondary"
changeMenu(n);
//-->
//]]>
</script>
</head>
<body>
</body>
</html>
[/code]
[code=php]
function changeMenu(idx) {
//change n
n=idx;
var Obj=document.MyForm.SelectSecondary;
//delete options
for(var i=Obj.options.length; i>=0; i--) {
Obj.options[i]=null;
}
//add options
for(i=0; i<subopt[idx].length; i++) {
Obj.options[i]=new Option(subopt[idx][i], i);
}
Obj.options[n2].selected=true;
}
[/code]
[code=php]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>dynamic select lists</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<script type="text/javascript">
//<![CDATA[
<!--
//small addition to the code
function changen2() {
if (n2!==0) {
n2=0;
}
}
function changeMenu(idx) {
//change n
n=idx;
var Obj=document.MyForm.SelectSecondary;
//delete options
for(var i=Obj.options.length; i>=0; i--) {
Obj.options[i]=null;
}
//add options
for(i=0; i<subopt[idx].length; i++) {
Obj.options[i]=new Option(subopt[idx][i], i);
}
Obj.options[n2].selected=true;
}
var n = 1; // initilize first select
var n2=1; // initilize second list
var catopt = ["Main Option 1","Main Option 2","Main Option 3"];
var subopt = [];
subopt[0] = ["Sub-option 1","Sub-option 2","Sub-option 3","Sub-option 4"];
subopt[1] = ["Sub-option 5","Sub-option 6"];
subopt[2] = ["Sub-option 7","Sub-option 8","Sub-option 9","Sub-option 10"];
var tbl='<FORM NAME="MyForm"><TABLE BORDER = 0 BGCOLOR = "#F8D85A" CELLPADDING = 3 CELLSPACING = 0>'+
'<TR><TD COLSPAN = 3><FORM NAME = "SelectMenu">'+
'<FONT SIZE = 4 FACE = "Times">'+
'</TD></TR><TR><TD ALIGN = LEFT>'+
'<FONT SIZE = 2 FACE = "Times" >Main Menu</FONT><BR>'+
'<SELECT NAME="SelectPrimary" onChange="changen2(this.options[this.selectedIndex].value); changeMenu(this.options[this.selectedIndex].value)">';
for(var i=0; i<catopt.length; i++) {
if(i==n) {
tbl+='<OPTION VALUE="'+i+'" SELECTED>'+catopt[i];
}
else {
tbl+= '<OPTION VALUE="'+i+'">'+catopt[i];
}
}
tbl+='</SELECT></TD><TD><FONT SIZE = 2 FACE = "Times" >Sub- Menu</FONT><BR>'+
'<SELECT NAME="SelectSecondary" onChange="n2=this.options[this.selectedIndex].value;alert("n2="+n2);">';
tbl+='</TD><TD VALIGN = BOTTOM></TD></TR></FORM></TABLE>';
document.write(tbl);
// Initilize "SelectSecondary"
changeMenu(n);
//-->
//]]>
</script>
</head>
<body>
</body>
</html>
[/code]
[code=php]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>dynamic select lists</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<script type="text/javascript">
//<![CDATA[
<!--
//small addition to the code
function changen2() {
if (n2!==0) {
n2=0;
}
}
function changeMenu(idx) {
//change n
n=idx;
var Obj=document.MyForm.SubStage;
//delete options
for(var i=Obj.options.length; i>=0; i--) {
Obj.options[i]=null;
}
//add options
for(i=0; i<subopt[idx].length; i++) {
Obj.options[i]=new Option(subopt[idx][i], subvalue[idx][i]);
}
Obj.options[n2].selected=true;
var TextDisplay = Obj.options[n2].value;
// *****************************************************
// help - how do I uncomment the line below and get it to work properly?
//document.getElementById('div1').firstChild.data=TextDisplay;
alert (TextDisplay);
}
// ******************************************************
function updatetext(TextDisplay) {
// *******************************************************
// help - how do I uncomment the line below and get it to work properly?
//document.getElementById('div1').firstChild.data=TextDisplay;
alert (TextDisplay);
}
// *****************************************************************
var n = 1; // initilize first select
var n2=1; // initilize second list
var catopt = ["Main Option 1","Main Option 2","Main Option 3"];
var subopt = [];
subopt[0] = ["Sub-option 1","Sub-option 2","Sub-option 3","Sub-option 4"];
subopt[1] = ["Sub-option 5","Sub-option 6"];
subopt[2] = ["Sub-option 7","Sub-option 8","Sub-option 9","Sub-option 10"];
var subvalue = [];
subvalue[0] = ["Sub-option value 1","Sub-option value 2","Sub-option value 3","Sub-option value 4"];
subvalue[1] = ["Sub-option value 5","Sub-option value 6"];
subvalue[2] = ["Sub-option value 7","Sub-option value 8","Sub-option value 9","Sub-option value 10"];
var tbl='<FORM NAME="MyForm"><TABLE BORDER = 0 BGCOLOR = "#F8D85A" CELLPADDING = 3 CELLSPACING = 0>'+
'<TR><TD COLSPAN = 3><FORM NAME = "SelectMenu">'+
'<FONT SIZE = 4 FACE = "Times">'+
'</TD></TR><TR><TD ALIGN = LEFT>'+
'<FONT SIZE = 2 FACE = "Times" >Main Menu</FONT><BR>'+
'<SELECT NAME="OverallStage" onChange="changen2(this.options[this.selectedIndex].value); changeMenu(this.options[this.selectedIndex].value)">';
for(var i=0; i<catopt.length; i++) {
if(i==n) {
tbl+='<OPTION VALUE="'+i+'" SELECTED>'+catopt[i];
}
else {
tbl+= '<OPTION VALUE="'+i+'">'+catopt[i];
}
}
tbl+='</SELECT></TD><TD><FONT SIZE = 2 FACE = "Times" >Sub- Menu</FONT><BR>'+
'<SELECT NAME="SubStage" onChange="n2=this.options[this.selectedIndex].value; updatetext(this.options[this.selectedIndex].value)">';
tbl+='</TD><TD VALIGN = BOTTOM></TD></TR></FORM></TABLE>';
document.write(tbl);
// Initilize "SubStage"
changeMenu(n);
//-->
//]]>
</script>
</form>
<div id ="div1"> </div>
</body>
</html>
[/code]
[code=php]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>dynamic select lists</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
</head>
<body onload="changeMenu(n);">
<script type="text/javascript">
//<![CDATA[
<!--
//small addition to the code
function changen2() {
if (n2!==0) {
n2=0;
}
}
function changeMenu(idx) {
//change n
n=idx;
var Obj=document.MyForm.SubStage;
//delete options
for(var i=Obj.options.length; i>=0; i--) {
Obj.options[i]=null;
}
//add options
for(i=0; i<subopt[idx].length; i++) {
Obj.options[i]=new Option(subopt[idx][i], subvalue[idx][i]);
}
Obj.options[n2].selected=true;
var TextDisplay = Obj.options[n2].value;
// *****************************************************
// help - how do I uncomment the line below and get it to work properly?
document.getElementById('div1').firstChild.data=TextDisplay;
alert (TextDisplay);
}
// ******************************************************
function updatetext(TextDisplay) {
// *******************************************************
// help - how do I uncomment the line below and get it to work properly?
document.getElementById('div1').firstChild.data=TextDisplay;
alert (TextDisplay);
}
// *****************************************************************
var n = 1; // initilize first select
var n2=1; // initilize second list
var catopt = ["Main Option 1","Main Option 2","Main Option 3"];
var subopt = [];
subopt[0] = ["Sub-option 1","Sub-option 2","Sub-option 3","Sub-option 4"];
subopt[1] = ["Sub-option 5","Sub-option 6"];
subopt[2] = ["Sub-option 7","Sub-option 8","Sub-option 9","Sub-option 10"];
var subvalue = [];
subvalue[0] = ["Sub-option value 1","Sub-option value 2","Sub-option value 3","Sub-option value 4"];
subvalue[1] = ["Sub-option value 5","Sub-option value 6"];
subvalue[2] = ["Sub-option value 7","Sub-option value 8","Sub-option value 9","Sub-option value 10"];
var tbl='<FORM NAME="MyForm"><TABLE BORDER = 0 BGCOLOR = "#F8D85A" CELLPADDING = 3 CELLSPACING = 0>'+
'<TR><TD COLSPAN = 3><FORM NAME = "SelectMenu">'+
'<FONT SIZE = 4 FACE = "Times">'+
'</TD></TR><TR><TD ALIGN = LEFT>'+
'<FONT SIZE = 2 FACE = "Times" >Main Menu</FONT><BR>'+
'<SELECT NAME="OverallStage" onChange="changen2(this.options[this.selectedIndex].value); changeMenu(this.options[this.selectedIndex].value)">';
for(var i=0; i<catopt.length; i++) {
if(i==n) {
tbl+='<OPTION VALUE="'+i+'" SELECTED>'+catopt[i];
}
else {
tbl+= '<OPTION VALUE="'+i+'">'+catopt[i];
}
}
tbl+='</SELECT></TD><TD><FONT SIZE = 2 FACE = "Times" >Sub- Menu</FONT><BR>'+
'<SELECT NAME="SubStage" onChange="n2=this.options[this.selectedIndex].value; updatetext(this.options[this.selectedIndex].value)">';
tbl+='</TD><TD VALIGN = BOTTOM></TD></TR></FORM></TABLE>';
document.write(tbl);
// Initilize "SubStage"
//-->
//]]>
</script>
</form>
<div id ="div1">text(node)</div>
</body>
</html>
[/code]
0.1.9 — BETA 5.11