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

problem rewriting option of a select list/menu

Status
Not open for further replies.

78588399

IS-IT--Management
Sep 2, 2010
1
NO
hello i have a probleme with a javascript that work on IE, safari,Chrome but doesn't in firefox or opera :

it is a simple script that erase and rewrite the content of 2 select list/menu from an 2 array.. I will apreciate some help on this one, here is the code that write the option of the select menu:
i will put all the script and highlight where is the problem:

<script language="javascript">//(arrays are empty for space)

var sub_cat_array = new Array(); //1st array for 2nd list
var sub_cat_array2 = new Array(); //2nd array for 3rd list

window.onload=function(){
document.getElementById("hidden_elements").style.display="none";
document.getElementById("hidden_elements2").style.display="none";
document.getElementById("markedslist").onchange=selcted;
document.getElementById("subcat1").onchange=selcted2;
}

function selcted(){
document.getElementById("hidden_elements").style.display="none";//reset
document.getElementById("hidden_elements2").style.display="none";//reset
if(document.getElementById("markedslist").value !="all"){
document.getElementById("hidden_elements").style.display="block";


}else{
document.getElementById("hidden_elements").style.display="none";
document.getElementById("hidden_elements2").style.display="none";
}
var markedslist=this.value;
var sub_cat=document.getElementById("subcat1");
sub_cat.options.length=0;

//probleme start here

for(var i=0;i< sub_cat_array.length; i++){
var st = sub_cat_array[0];
if(st==markedslist){
var opt = new Option(sub_cat_array[1]);
// var val = sub_cat_array[2];
opt.value=sub_cat_array[2];
try{
sub_cat.add(opt,opt.value);
}catch(e){
sub_cat.add(opt);
}
}

}
} // probleme finish here and it is the same for the function selcted2 below


function selcted2(){
if(document.getElementById("subcat1").value != "all"){
document.getElementById("hidden_elements2").style.display="block";
}else{ document.getElementById("hidden_elements2").style.display="none";
}
var subcat1=this.value;
var sub_cat2=document.getElementById("subcat2");
sub_cat2.options.length=0;

for(var i=0;i< sub_cat_array2.length; i++){
var st2 = sub_cat_array2[0];
if(st2==subcat1){
var opt2 = new Option(sub_cat_array2[1]);
//var val2 = sub_cat_array2[2];
opt2.value=sub_cat_array2[2];
try{
sub_cat2.add(opt2,opt2.value);
}catch(e){
sub_cat2.add(opt2);
}
}

}
}
</script>
 
This comes from the selcted(), same correction for selct2().
[tt]
for(var i=0;i< sub_cat_array.length; i++){
var st = sub_cat_array[0];
if(st==markedslist){
[red]//[/red]var opt = new Option(sub_cat_array[1]);
var opt = new Option(sub_cat_array[1][red], sub_cat_array[2][/red]);
//var val = sub_cat_array[2];
[red]//[/red]opt.value=sub_cat_array[2];
[blue]/* or equally valid lines (cross-browser)
var opt=new Option();
opt.text=sub_cat_array[1];
opt.value=sub_cat_array[2];
*/[/blue]
try{
[red]//[/red]sub_cat.add(opt,opt.value);
sub_cat.[red]options.[/red]add([red]opt[/red]); //add options reference to be cross-browser
[blue]/* or equally valid lines (cross-browser)
sub_cat[sub_cat.length]=opt;
//or
//sub_cat.options[sub_cat.length]=opt;
//or some variation still use sub_car.options.length
*/[/blue]
}catch(e){
[red]//[/red]sub_cat.add(opt);
//shouldn't have any, if array is properly defined
}
}

}
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top