<p>I’ve been seeing the trend of applying inline labels on form elements more and more these days. So I definitely needed to come up with a solid solution that would help me easily apply this effect.</p>

<p><a href="//assets.trevor-davis.com/play/jquery-inline-form-labels/"><img src="//assets.trevor-davis.com/uploads/images/content/2009/06/jquery-inline-labels.png" alt="jQuery Inline Form Labels" width="280" height="169" class="leftSide" /></a>Maybe I need to explain a little better what I am referring to. Instead of using a label that is next to or above the input element, the label is actually within the input. Then, when the input is focused, the label goes away and you can enter your value into it. Finally, when the input is no longer focused, if there is no value entered, the inline label is then added back in.</p>

<p><a href="//assets.trevor-davis.com/play/jquery-inline-form-labels/" class="more-link">See Demo</a></p>

<h2>My Solution</h2>
<p>The title attribute.</p>

<p>It’s so simple, but perfect. A great benefit of using the title attribute, is that when you hover over the input, you get a little tooltip displaying the inline label. So all I need to do is add a title attribute with the text that we want to have show up as the inline label. Then, we just use a little jQuery to make it all happen.</p>

<h3>The jQuery</h3>

<p>First, we want to select each input that has a title attribute, once the document is loaded:</p>

<div class="codeblock"><code>
$(document).ready(function() {<br /> $('input[title]').each(function() {<br /> &amp;hellip;<br /> });<br />}); 

</code></div>

<p>Next, if the value of the input element is empty, we want to take the title attribute and add it as the value of the input.</p>

<pre>$(document).ready(function() {
 $('input[title]').each(function() {
  <strong>if($(this).val() === '') {
   $(this).val($(this).attr('title')); 
  }</strong>
 });
});</pre>

<p>After that, we want to setup a function that will fire once the input is focused. When it is focused, if the value is equal to the title attribute, we want to set the value to nothing and add a class of focused:</p>

<pre>$(document).ready(function() {
 $('input[title]').each(function() {
  if($(this).val() === '') {
   $(this).val($(this).attr('title')); 
  }
  
  <strong>$(this).focus(function() {
   if($(this).val() === $(this).attr('title')) {
    $(this).val('').addClass('focused'); 
   }
  });</strong>
 });
});</pre>

<p>The reason why we add the class of focused is so that we can change the text color, border or anything to make it noticeable that the input is being focused. You can see this effect in the <a href="//assets.trevor-davis.com/play/jquery-inline-form-labels/">demo</a>.

</p><p>Finally, we want to add a function that will fire when the input loses focus. When it does, if the input value is empty, we want to set the value back to the title attribute and remove the class of focused:</p>

<pre>$(document).ready(function() {
 $('input[title]').each(function() {
  if($(this).val() === '') {
   $(this).val($(this).attr('title')); 
  }
  
  $(this).focus(function() {
   if($(this).val() === $(this).attr('title')) {
    $(this).val('').addClass('focused'); 
   }
  });
  
  <strong>$(this).blur(function() {
   if($(this).val() === '') {
    $(this).val($(this).attr('title')).removeClass('focused'); 
   }
  });</strong>
 });
});</pre>

<p><a href="//assets.trevor-davis.com/play/jquery-inline-form-labels/" class="more-link">See Demo</a></p>

<p>And that’s it. It’s just a little bit of code, but it can go a long way. So take it and build on it.</p>