Created
July 9, 2009 18:58
-
-
Save gf3/143893 to your computer and use it in GitHub Desktop.
Hidden elements on a page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Many times, when creating a site, I need hidden elements on a page that I must later make visible. Of course it would | |
be great if one could simply hide them via CSS, and later [easily] show them via javascript. However this is not the | |
case, javascript has some difficulties overriding styles set in a stylesheet. So, I've found the best way to | |
accomplish this is to use a combo of CSS and javascript in a two-step process: | |
1. Hide the elements via CSS class – so they are hidden when the user first sees the page | |
2. Hide the elements via javascript and remove the corresponding CSS class, after the DOM has loaded. | |
The second step has the potential to be quite expensive on the DOM, but I feel that it's an acceptable trade-off | |
because the elements in question are already hidden. This way they are ready to be manipulated, one doesn't need to | |
worry about fiddling with CSS classes or styles. | |
## Stylesheet | |
.hidden { | |
display: none; | |
} | |
## Javascript | |
// Prototype | |
document.observe('dom:loaded', function(){ | |
$$('.hidden').each(function(i) { i.hide().removeClassName("hidden"); }); | |
}); | |
// jQuery | |
jQuery(function() { | |
jQuery('.hidden').css('display', 'none').removeClass('hidden'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment