Tuesday, November 9, 2010

Internet Explorer empty element headache - a quick fix with jQuery

Internet Explorer 7 doesn't render an empty element right, when this element has some layout features (for example, background). The element is supposed to be rendered as it were non existent on the page, instead we have an annoying box with non-zero dimensions. It's frustrating when you have on your page a message box for potential error and success messages, and when there are no messages, you still see it in IE.

The solution is to hide the box when there is no text in it.

$j(".message-box").each(function() {
if(jQuery.trim($j(this).text()).length == 0) {
$j(this).hide();
}
});


If the page is rendered with a message, then the box is shown. If there is a message rendered over AJAX, then we rerender the whole message box and it's visible originally.

No comments:

Post a Comment