Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can the label tags for attribute be associated with a normal div?

is it valid html under html5 definitions to use a label's "for" value as an id of a normal div element (for example I have made a custom dropdown list implementation which is encased inside a div)

Please let me know if possible,

Thomas

like image 364
Thomas Avatar asked Jul 29 '12 01:07

Thomas


People also ask

Can we use label for div?

Div is a block element, so cannot go inside a label which will only accept Phrasing content. You may use <span> instead as that is an in-line element.

What does the for attribute in a label tag do?

The HTML <label> for Attribute is used to specify the type of form element a label is bound to. Attribute Values: It contains the value i.e element_id which specify the id of the element that the label is bound to.

Does label tag need for attribute?

Yes, but that doesn't mean the for attribute is required. For example, if the input is nested inside the label, the label is “for” that input implicitly. It's always a good idea to give a label a 'for' attribute and link it to the control's ID.

Which HTML attribute allows us to label div elements?

The for attribute specifies which form element a label is bound to.


2 Answers

Not according to the spec:

Some elements, not all of them form-associated, are categorized as labelable elements. These are elements that can be associated with a label element.

"button" "input" (if the type attribute is not in the hidden state) "keygen" "meter" "output" "progress" "select" "textarea"

http://www.w3.org/TR/html5/forms.html#category-label

See also:

The for attribute may be specified to indicate a form control with which the caption is to be associated. If the attribute is specified, the attribute's value must be the ID of a labelable element in the same Document as the label element. If the attribute is specified and there is an element in the Document whose ID is equal to the value of the for attribute, and the first such element is a labelable element, then that element is the label element's labeled control.

http://www.w3.org/TR/html5/the-label-element.html#attr-label-for

I do think the question presents a valid use case. I'm not sure what the recommended pattern is for such a scenario, though ARIA attributes might help to make the markup more accessible:

https://developer.mozilla.org/en/Accessibility/ARIA/forms/Basic_form_hints https://developer.mozilla.org/Special:Tags?tag=ARIA

like image 156
Tim M. Avatar answered Sep 20 '22 09:09

Tim M.


As Tim noted it is not possible to do it natively, however with a little bit of javascript you can simulate it

jQuery(document).delegate('[for]','click',function(e){     var targetEl = jQuery('#'+jQuery(this).attr('for'));     if(targetEl.is('div.your-custom-dropdown-class')) { //if targetEl is one of your dropdowns         e.preventDefault();         targetEl.trigger('click'); //open the drop down     } }); 
like image 22
Vatev Avatar answered Sep 24 '22 09:09

Vatev