Forum Moderators: not2easy

Message Too Old, No Replies

Hiding form labels in IE

"display: none" hides the whole form element

         

SilverLining

5:04 pm on Sep 15, 2006 (gmt 0)

10+ Year Member



For accessibility purposes I have added form labels to all form elements on html pages. Quite a few of these text labels should be hidden from the visible eye.

Try #1
I set labels to display: none. This works perfectly in Firefox, but in IE the whole dropdown disappears.

Try #2
Added CSS to the labels to display left, but this does not work in IE either. The whole form element still disappears.


position:absolute;
left: -9999px;

Try #3
Added a class="superfluous" to the labels I want hidden. Then some JS to hide with the DOM:


<script type="text/javascript">
function addEvent(elm, evType, fn,useCapture)
{
if(elm.addEventListener)
{
elm.addEventListener(evType, fn, useCapture);
return true;
}
else if (elm.attachEvent)
{
var r = elm.attachEvent('on' + evType, fn);
return r;
}
else
{
elm['on' + evType] = fn;
}
}
function removeSuperfluousLabels()
{
//get all labels
var labels = document.getElementsByTagName("label");
for (i=0;i<labels.length;i++)
{
//look for all labels that are classed as superfluous
if(labels[i].className=="superfluous")
{
//hide the label
labels[i].style.display="none";
}
//add in default text to show format required
//find the field that the label refers to (using for attribute)
var thisId = labels[i].getAttribute("for");
if ((thisId)==null)
{//alert('feckin ie') ... as ever
thisId = labels[i].htmlFor;
}
var format = labels[i].childNodes[0].nodeValue;
//find the format suggested - in parenthesis following word 'format'
//oh, and did I tell you that I'm useless at writing/deciphering regexp?!
var startpoint = format.indexOf("(format ")+ 8;
var endpoint = format.indexOf(")");
format=format.substr(startpoint,(endpoint-startpoint));
//write the format into the text input field
document.getElementById(thisId).value=format;
//add in auto-select behaviour to form fields so that user does not need to highlight to overtype (DOM-added) default text
//addEvent(document.getElementById(thisId), 'focus', autoselect, false); // doesn't work because of 'this' keyword misunderstood by IE in other function
document.getElementById(thisId).onfocus = autoselect;
}
}
function autoselect()
{
this.select();
}
addEvent(window, 'load', removeSuperfluousLabels, false);
</script>

All three times the elements are not displayed in IE. Any fixes for this? Ideally I would like to get this sorted by using solely CSS, without any JS, because the JS is only applied client side, so you'll see the labels display and then disappear.

Thanks.

Robin_reala

6:14 pm on Sep 15, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can definitely do this. Could you post your HTML and CSS? Incidentally, the 'off left' method is the best in this situation.

SilverLining

9:21 am on Sep 18, 2006 (gmt 0)

10+ Year Member



Thanks for steering me into the right direction, Robin. I fixed it by adding 'off left' CSS to the "superfluous" class (when the JS was removed) instead of the label element. SL