* Note: These examples reference the Prototype JavaScript framework.
Often I find that I want to load content onto a page but have its default view state as hidden until a user defined event makes it appear.
The problem is if I hide an element using a linked CSS file (with a class or Id style of {display:none;}) it seems I cannot show that element using either of the following JavaScript options:
Where the CSS class ’showElement’ above is basically {display:block;}
Now if I add the CSS to hide the element inline like so:
As far as I can see, this seems to be a problem with Prototype, rather than JS/CSS as a whole.
I have done exactly what you describe using jQuery, and it works perfectly, and as expected.
You mention having to use your workaround in jQuery too - have you actually tried it in jQuery, or did you assume it would behave the same way as Prototype (of which, for the record, I have no working experience…)?
Seb Duggan
March 11th, 2008
I must admit this particular scenario I’ve only had to do when using Prototype.
I have of course manipulated stylesheets with jQuery, just not with hiding/showing elements.
That is great news that it is possible with jQuery.
I’ll have to give it a go when I get a chance.
Thanks
Michael Sharman
March 11th, 2008
The way I’ve been handling this case is similarly using the onload property of the DOM window object. I’m curious whether it’s functionally exactly the same as your solution — if so, this code is slightly shorter:
window.onload = init;
function init() {
$('myEl').hide();
}
Also, for the record, this will work with jQuery with one tiny modification. Add the pound sign for selecting an ID:
$('#myEl').hide();Zachary Crockett
April 19th, 2008
@Zac - Yep, jQuery will show an element which has been hidden using a style in an external .css file (display:none;).
Not really sure why Prototype has problems with this, hence the workaround.
I assume you’re not using “window.onload” if you have a JS framework in place? That would be much slower than using the “DOM ready” approach that either Prototype or jQuery provide.
Michael Sharman
April 19th, 2008