View Single Post
Old 06-18-2003, 06:57 PM   PM User | #8
beetle
Senior Coder

 
Join Date: Aug 2002
Posts: 3,467
Thanks: 0
Thanked 0 Times in 0 Posts
beetle has a little shameless behaviour in the past
Here's the updated version. It handles all form controls and has a few other fault tolerance measures. I also commented it
Code:
function addLabelProperties( f )
{
    //    Collect all label elements in form, init vars
    if ( typeof f.getElementsByTagName == 'undefined' ) return;
    var labels = f.getElementsByTagName( "label" ),
        label,
        elem,
        i = j = 0;

    //    Loop through labels retrieved
    while ( label = labels[i++] )
    {            
        //    For Opera 6
        if ( typeof label.htmlFor == 'undefined' ) return;
        
        //    Retrieve element
        elem = f.elements[label.htmlFor];

        if ( typeof elem == 'undefined' )
        {    //    No element found for label
            alert( "No element found for label: " + label.htmlFor );
        }
        else if ( typeof elem.label != 'undefined' )
        {    //    label property already added
            continue;
        }
        else if ( typeof elem.length != 'undefined' && elem.length > 1 && elem.nodeName != 'SELECT' )
        {    //    For checkbox arrays and radio-button groups
            for ( j = 0; j < elem.length; j++ )
            {
                elem.item( j ).label = label;                    
            }
        }
        //    Regular label
        elem.label = label;            
    }
}
This is the version of this function I'm using in fValidate - and it works great!
__________________
My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
“Minds are like parachutes. They don't work unless they are open”
“Maturity is simply knowing when to not be immature”
beetle is offline   Reply With Quote