What's new

Welcome to the forum 👋, Visitor

To access the forum content and all our services, you must register or log in to the forum. Becoming a member of the forum is completely free.

Help Clarify Some Things about <label> and <input> Please

Ahr Aitch

New member
Joined
Aug 12, 2023
Messages
11
Reaction score
0
HTML Coins
14
Consider the following code:

<fieldset>
<legend>This area for entering informaton about marriages</legend>

<label for="marriagePeopleID" id="marriagePeopleID" class="required">
Marriage of:
</label>
<input type="text" name="peopleFirstName" placeholder="First" required size="10">
<input type="text" name="peopleMiddleName" placeholder="Middle" size="10">
<input type="text" name="peopleLastName" required placeholder="Last" size="10">

</fieldset>

I don't understand why both the for and the id attributes are included in the <label> tag pair. It's my understanding that they are equivalent and mean the same thing and serve to relate the information entered in the <input . . . > tags to dataelements defined by the name="peopleFirstName" and name="peopleMiddleName" and name="peopleLastName". My confusion lies in 3 things: a) why do for and id, meaning and used to do the same thing, occur in the <label ...> statement; and b) there is no element in the <input . . . > statements that do so; and c) there is nothing in the <label . . . > statement that ties the values entered for the 3 parts of the name to the <label>. The code works. It formats a screen correctly. It passes the HTML code validator. I just don't understand how. I got here because I originally had the id="marriagePeopleID" in each of the 3 <input . . . > statements. The validator didn't pass that arrangement.

Now, the HTML code validator is giving me the message: The “for” attribute of the “label” element must refer to a non-hidden form control. I don't understand this at all, probably because what I stated in the paragraph above is incorrect, that for and id don't work the way I think. My code and the CSS that styles it in no way have variable or data element declarations. IOW, there is no way for the code validator to know that marriagePeopleID and peopleFirstName have any relationship to anything, much less to each other. Why does it imply that the first, marriagePeopleID , is a hidden form control? What has to be done to un-hide it? If it's purpose is merely to relate the label with the input, why does it matter? I know that every for and id declaration has to be unique within the document. That is not the problem. I have not used it anywhere else. VS Code helps keep me honest in that case.

In the next part of the code, after that shown as an example, I have code that strings city, county, state, and country together in a similar fashion, except that the code validator takes no exception to the (unique) qualifier in the for attribute. That code is followed by:
<select name="spouseBirthState">
<option value="AL">AL</option>
<option value="AR">AR</option>
<option value="AS">AS</option>
<option value="AZ">AZ</option>
.
.
.
</select>
This code works just fine without a for or id attribute, which I don't understand at all.

Before closing, I should add that I am a complete novice. I am designing frontend screens without any validation code so far. I felt I needed to get the HTML and CSS worked out before I dove into javascript or other frontend data validation scripting. Backend scripting and SQL will come when I dive into javascript, php, etc. For now, I'd like a better understanding of the concepts above. FWIW, I've spent hours on tutorials and reading textbooks. Nowhere have I found anything that answers the questions I've posed here, and that includes a couple of hours reading hits from searching.

I'd be grateful any light anyone can shed on this.
I have 10 forms all using <label> and <input> in the same way. I need to understand this so that I can fix the problem in all of them before I start validating the code.
 

Theme customization system

You can customize some areas of the forum theme from this menu.

  • Wide/Narrow view

    You can control a structure that you can use to use your theme wide or narrow.

    Grid view forum list

    You can control the layout of the forum list in a grid or ordinary listing style structure.

    Picture grid mode

    You can control the structure where you can open/close images in the grid forum list.

    Close sidebar

    You can get rid of the crowded view in the forum by closing the sidebar.

    Fixed sidebar

    You can make it more useful and easier to access by pinning the sidebar.

    Close radius

    You can use the radius at the corners of the blocks according to your taste by closing/opening it.

  • Choose the color combination that reflects your taste
    Background images
    Color gradient backgrounds
Back