darksarin
10-19-2004, 03:10 PM
I have a script that works, but there are certain areas that I am still trying to fix. This script is borderline for the DOM or general section, but I think this is the better place to put it.
I have a form with a select box that I need to use to control the appearance of other select boxes. As with good programming, I am trying to make this general enough to reuse in various places on the page.
Here is the script:
function changeDiv(opt_group, display){
var the_div = getOptionSelected(opt_group);
alert(the_div);
var the_style = getStyleObject(the_div);
if (the_style != false){
the_style.display = display;
}
}
function getOptionSelected(opt_group){
if(document.getElementById && document.getElementById(opt_group)){
alert(document.getElementById(opt_group).value);
}
else if (document.all && document.all(opt_group)){
alert(document.ads.all(opt_group).value);
}
else {
return false;
}
}
function hideAll(){
changeDiv("radio","none");
changeDiv("print","none");
changeDiv("tv","none");
changeDiv("internet","none");
changeDiv("other","none");
}
function getStyleObject(objectId){
if (document.getElementById && document.getElementById(objectId)){
return document.getElementById(objectId).style;
}
else if (document.all && document.all(objectId)){
return document.all(objectId).style;
}
else {
return false;
}
}
// -->
I am only having trouble with the getOptionSelected() function--the others work as designed (which is to dynamically hide/display divs). Note that I am currently using the alert() for bug-testing, instead of returning the values. When I do this, about 12 alert boxes pop up (probably based on the number of named elements on the page?), and in IE one of them will have the actual value of the selected item in the select box. Firefox/Mozilla just has a bunch of alert boxes that say either "undefined" or "false".
If necessary, I can post the related html, but for the sake of brevity, I will wait and see if that's important. I am not a big javascript programmer, but I am learning!
Thanks in advance for any help.
:rolleyes:
I have a form with a select box that I need to use to control the appearance of other select boxes. As with good programming, I am trying to make this general enough to reuse in various places on the page.
Here is the script:
function changeDiv(opt_group, display){
var the_div = getOptionSelected(opt_group);
alert(the_div);
var the_style = getStyleObject(the_div);
if (the_style != false){
the_style.display = display;
}
}
function getOptionSelected(opt_group){
if(document.getElementById && document.getElementById(opt_group)){
alert(document.getElementById(opt_group).value);
}
else if (document.all && document.all(opt_group)){
alert(document.ads.all(opt_group).value);
}
else {
return false;
}
}
function hideAll(){
changeDiv("radio","none");
changeDiv("print","none");
changeDiv("tv","none");
changeDiv("internet","none");
changeDiv("other","none");
}
function getStyleObject(objectId){
if (document.getElementById && document.getElementById(objectId)){
return document.getElementById(objectId).style;
}
else if (document.all && document.all(objectId)){
return document.all(objectId).style;
}
else {
return false;
}
}
// -->
I am only having trouble with the getOptionSelected() function--the others work as designed (which is to dynamically hide/display divs). Note that I am currently using the alert() for bug-testing, instead of returning the values. When I do this, about 12 alert boxes pop up (probably based on the number of named elements on the page?), and in IE one of them will have the actual value of the selected item in the select box. Firefox/Mozilla just has a bunch of alert boxes that say either "undefined" or "false".
If necessary, I can post the related html, but for the sake of brevity, I will wait and see if that's important. I am not a big javascript programmer, but I am learning!
Thanks in advance for any help.
:rolleyes: