potatoDie Web Development Scrapbook
Assorted thoughts about web development
Hiding elements from users using javascript, with hidden hiding
document.write() does not belong to my favourite javascript functions, but sometimes it is useful.
Suppose you have an element on a webpage that’s just there for fallback for users without javascript. For example a form that submits itself at onchange or onclick events of it’s elements.
Removing the button after the page is loaded (which is easy: element.style.display=’none’) may cause noise on the page. It’s there, then it’s gone, maybe causing containers to be redrawn too.
Changing the rules of a CSS file is cumbersome (http://www.quirksmode.org/dom/changess.html).
So in this case I use document.write() to ‘manually’ add a style for javascript users:
document.write("<style>#submit{display:none}</style>");
Take care this script is run as early as possible. Writing a link to a separate CSS file is also possible, of course.
jQuery
Using the jQuery ready handler, the hiding of the elements also seems to be in time (you won’t see it).