Monday, March 1, 2010

showOrHide algorithm

Making it easier for me to look up my own most useful block of code for displaying/hiding elements. This is the same bit of code I wrote for Microsoft that shipped with Vista and now Windows 7 with the Sidebar gadgets.

////////////////////////////////////////////////////////////////////////////////
//
// showOrHide([object|string] oHTMLElement, boolean bShowOrHide)
// Shows or Hides an HTMLElement.
// You can pass in the id to an object or the actual object
//
////////////////////////////////////////////////////////////////////////////////
function showOrHide(oHTMLElement, bShowOrHide) {
try {
if (typeof(oHTMLElement)=="string") {
oHTMLElement = document.getElementById(oHTMLElement);
}
if (oHTMLElement && oHTMLElement.style) {
if (bShowOrHide == 'inherit') {
oHTMLElement.style.visibility = 'inherit';
} else {
if (bShowOrHide) {
if (oHTMLElement.nodeName == 'TR') {
oHTMLElement.style.visibility = 'inline-table';
} else {
oHTMLElement.style.visibility = 'visible';
}
} else {
oHTMLElement.style.visibility = 'hidden';
}
try {
if (bShowOrHide) {
oHTMLElement.style.display = 'block';
} else {
oHTMLElement.style.display = 'none';
}
}
catch (ex) {
}
}
}
}
catch (ex) {
}
}

Shannon Norrell


Now on GitHub