Is there a more elegant solution than an if-statement with no else clause?
Posted
by Jay
on Stack Overflow
See other posts from Stack Overflow
or by Jay
Published on 2010-04-08T21:02:53Z
Indexed on
2010/04/08
21:13 UTC
Read the original article
Hit count: 217
In the following code, if Control
(the element that trigers Toggle
's first OL
) is not Visible
it should be set Visible
and all other Controls
(Controls[i]
) so be Hidden
.
.js
function Toggle(Control){
var Controls=document.getElementsByTagName("ol",document.getElementById("Quote_App"));
var Control=Control.getElementsByTagName("ol")[0];
if(Control.style.visibility!="visible"){
for(var i=0;i<Controls.length;i++){
if(Controls[i]!=Control){
Reveal("hide",20,0.3,Controls[i]);
}else{
Reveal("show",20,0.3,Control);
};
};
}else{
Reveal("hide",20,0.3,Control);
};
};
Although the function [Toggle
] works fine, it is actually setting Controls[i]
to Hidden
even if it is already.
This is easily rectified by adding an If
statement as in the code below, surely there is a more elegant solution, maybe a complex If
condition?
.js
function Toggle(Control){
var Controls=document.getElementsByTagName("ol",document.getElementById("Quote_App"));
var Control=Control.getElementsByTagName("ol")[0];
if(Control.style.visibility!="visible"){
for(var i=0;i<Controls.length;i++){
if(Controls[i]!=Control){
if(Controls[i].style.visibility=="visible"){
Reveal("hide",20,0.3,Controls[i]);
};
}else{
Reveal("show",20,0.3,Control);
};
};
}else{
Reveal("hide",20,0.3,Control);
};
};
Your help is appreciated always.
© Stack Overflow or respective owner