# Is "label for" better?

**URL:** <https://www.ruby-forum.com/t/is-label-for-better/96840>\
**Category:** Rails\
**Created:** [July 2, 2006, 3:31pm UTC](https://www.ruby-forum.com/t/is-label-for-better/96840 "2006-07-02T15:31:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ldk2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d9b06d/32.png) [@ldk2005](https://www.ruby-forum.com/u/ldk2005)\
**Post date:** [July 2, 2006, 3:31pm UTC](https://www.ruby-forum.com/t/is-label-for-better/96840/1 "2006-07-02T15:31:03Z")

</div>

I’ve noticed that it is common practise in forms to use:

\<label for=“tablename”,“columnname”\> …Some column name

Why is the above construct better than just writting it this way?

Some column name

If there is not compelling reason, I’d prefer to use the second example  
and  
save on typing.

---

<div class="post-metadata">

**Author:** ![ldk2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d9b06d/32.png) [@ldk2005](https://www.ruby-forum.com/u/ldk2005)\
**Post date:** [July 2, 2006, 3:44pm UTC](https://www.ruby-forum.com/t/is-label-for-better/96840/2 "2006-07-02T15:44:20Z")

</div>

> \<label for=“tablename”,“columnname”\> …Some column name
> 
> Why is the above construct better than just writting it this way?  
> The ‘for’ attribute specifies which input field the label refers to.  
> This is an HTML thing rather than specifically Rails.

It’s an accessbility feature, generally important for voice and text  
browers.

Also, if you click a label that has a ‘for’ attribute, the assocatied  
input field will be selected.

Steve

---

<div class="post-metadata">

**Author:** ![ldk2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d9b06d/32.png) [@ldk2005](https://www.ruby-forum.com/u/ldk2005)\
**Post date:** [July 2, 2006, 6:02pm UTC](https://www.ruby-forum.com/t/is-label-for-better/96840/3 "2006-07-02T18:02:20Z")

</div>

OK, its worth a little extra typing for accessibility.

Is there a way to change the associated input field’s background color ?  
-Larry

---

<div class="post-metadata">

**Author:** ![ldk2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d9b06d/32.png) [@ldk2005](https://www.ruby-forum.com/u/ldk2005)\
**Post date:** [July 10, 2006, 2:12pm UTC](https://www.ruby-forum.com/t/is-label-for-better/96840/4 "2006-07-10T14:12:42Z")

</div>

you could hack together support for it in internet explorer using event  
listeners (onfocus, etc), temporarily adding a “focus” classname while  
the form element is focused. the last part is pretty simple with  
prototype, ie:

Element.classNames( “my\_form\_input” ).add( “focus” );

and the CSS:

input.focus, textarea.focus {  
background-color: #ffc;  
}

---

<div class="post-metadata">

**Author:** ![ldk2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d9b06d/32.png) [@ldk2005](https://www.ruby-forum.com/u/ldk2005)\
**Post date:** [July 10, 2006, 1:25pm UTC](https://www.ruby-forum.com/t/is-label-for-better/96840/5 "2006-07-10T13:25:35Z")

</div>

On 02 Jul 2006, at 18:00, Larry K. wrote:

> OK, its worth a little extra typing for accessibility.
> 
> Is there a way to change the associated input field’s background  
> color ?

input:focus, textarea:focus {  
background-color: #ffc;  
}

or just  
input, textarea {  
background-color: #ffc;  
}

That’s for the browsers that support it of course (i.e. you can  
forget about :focus on Internet Exploder)

Best regards

Peter De Berdt
