Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error validating HTML: The for attribute of the label element must refer to a form control

I don't know why I keep getting this error while checking my page at http://validator.w3.org/check The error was:

Line 46, Column 68: The for attribute of the label element must refer to a form control. 
<label class="environment-label" for="environment_form">Environments:</label>

I believe I provided an id reference for my label to the outer form, why it keep bugging me about this error?

<div>
    <form id="environment_form" method="post">
        <div class="styled-select">
            <label class="environment-label" for="environment_form">Environments:</label>
            <select name="environment_dropdown" onchange="selectionChanged()">
                <option @(ViewData["selection"] == null || string.IsNullOrEmpty(ViewData["selection"].ToString()) ? "selected" : "")>select one</option>
                @foreach (string name in Model) { 
                    <option @(ViewData["selection"] != null && ViewData["selection"].Equals(name) ? "selected" : "")> 
                        @name
                    </option>
                }
            </select> 
        </div>
    </form>
</div>
like image 605
Chan Avatar asked Jul 19 '12 18:07

Chan


People also ask

What are HTML form controls?

An HTML form is a section of a document which contains controls such as text fields, password fields, checkboxes, radio buttons, submit button, menus etc. An HTML form facilitates the user to enter data that is to be sent to the server for processing such as name, email address, password, phone number, etc. .

Which HTML form control attribute is used to reflect state value in the control itself?

The for attribute associates a label with another control explicitly: the value of the for attribute must be the same as the value of the id attribute of the associated control element. More than one LABEL may be associated with the same control by creating multiple references via the for attribute.

Which attribute of input tag specifies what type of form control will be added to the form area?

form: The form attribute is used to specify one or more forms to which the <input> element belongs to. max : The max attribute is used to specify the maximum value for an < input > element. required: The required attribute specifies that an input field must be filled out before submitting the form.

Which attribute must be unique within the HTML document *?

When specified on HTML elements, the id attribute value must be unique amongst all the IDs in the element's tree and must contain at least one character.


1 Answers

you have this :

for="environment_form"

and it refers to the form directly ! But the "for" attribute should refer to an element of your form, in your case to the select. So add an "id" attribute to your select and change the "for", like this fo example :

<label class="environment-label" for="environment_dropdown">Environments:</label>
<select name="environment_dropdown" id="environment_dropdown" onchange="selectionChanged()">
like image 76
PoulsQ Avatar answered Nov 02 '22 20:11

PoulsQ