user image

Dev-Mo
Published in : 2022-02-11

What is the need for using (for) attr in the label of an input tag?

HTML & CSS

I've been learning lately about the "for" attr in HTML and what it accomplishes, but I came across a strange example that I'm still trying to figure out.
I understand why "for" is required in the first block of code, but I'm not sure why "for" and "id" were used implicitly in the second code when it might have worked just fine without them. Is there anyone who can assist me?

 

<input id="here" type=radio name=here-there><label for="here">Here</label>

 

<label for="hello"><input id="hello" type=checkbox name=greeting> Hello</label><br><label><input type=checkbox name=greeting> Hello</label>

 

Comments

Mohamed Atef Date : 2022-02-11

Best answers

51

Best answers

51

For is used to indicate to the input which contain the id of the “for” value of the label for example in this code

<input id="hello" type=checkbox name=greeting><label for="hello"> Hello</label>

as you see in this code input is outside the label tag but when you click on the label “Hello” it will be the same as clicking on the checkbox itself

Dev-Mo Date : 2022-02-11

I can’t thank you enough!

Leave a comment

Join us

Join our community and get the chance to solve your code issues & share your opinion with us

Sign up Now

Related posts

Can't move the background-image code from CSS file to HTML file.
Publish date: 2022-02-13 | Comments: 2

Tag: HTML & CSS

How to align an Textarea in the middle of the webpage
Publish date: 2022-02-27 | Comments: 1

Tag: HTML & CSS

How to horizontally align to the centre a li inside an ul?
Publish date: 2022-03-04 | Comments: 2

Tag: HTML & CSS

Is OOCSS still a good CSS architecture in 2022?
Publish date: 2022-02-08 | Comments: 2

Tag: HTML & CSS

How to separate the background into three divs that can be flipped?
Publish date: 2022-02-12 | Comments: 1

Tag: HTML & CSS

Invalid CSS after : expected expression was - SASS error
Publish date: 2022-02-13 | Comments: 1

Tag: HTML & CSS

How do custamize my button to give it a bubble look using CSS?
Publish date: 2022-03-04 | Comments: 0

Tag: HTML & CSS

min-height is not working on td or th
Publish date: 2021-10-20 | Comments: 1

Tag: HTML & CSS