Sliding Labels v2 – Patch

Please use version 3 of sliding labels with updated options and bug fixes at: http://www.csskarma.com/blog/sliding-labels-plugin/

Last week I wrote an article about sliding form labels that got quite a bit of attention. Many of the commenters brought up a couple good points/bug in the Sliding Label code that I wanted to address and provide a patch for:

  • The sliding label was barfing out in Safari when auto-fill was active
  • The default scripting didn’t work on textareas

I sat down yesterday and wrote a patch/new version of sliding labels which I think addresses these two problems.

View the Demo

The new jQuery

$(function(){
$('form#info .slider label').each(function(){
	var labelColor = '#999';
	var restingPosition = '5px';

	// style the label with JS for progressive enhancement
	$(this).css({
		'color' : labelColor,
		 	'position' : 'absolute',
	 		'top' : '6px',
			'left' : restingPosition,
			'display' : 'inline',
    		        'z-index' : '99'
	});

	var inputval = $(this).next().val();

	// grab the label width, then add 5 pixels to it
	var labelwidth = $(this).width();
	var labelmove = labelwidth + 5 +'px';

	// onload, check if a field is filled out, if so, move the label out of the way
	if(inputval !== ''){
		$(this).stop().animate({ 'left':'-'+labelmove }, 1);
	}    	

	// if the input is empty on focus move the label to the left
	// if it's empty on blur, move it back
	$('input, textarea').focus(function(){
		var label = $(this).prev('label');
		var width = $(label).width();
		var adjust = width + 5 + 'px';
		var value = $(this).val();

		if(value == ''){
			label.stop().animate({ 'left':'-'+adjust }, 'fast');
		} else {
			label.css({ 'left':'-'+adjust });
		}
	}).blur(function(){
		var label = $(this).prev('label');
		var value = $(this).val();

		if(value == ''){
			label.stop().animate({ 'left':restingPosition }, 'fast');
		}	

	});
}); // End "each" statement
}); // End loaded jQuery

Textarea HTML

The HTML for the textarea follows the same convention as the rest of the inputs, in only needing a wrapping element.

<div id="comment-wrap"  class="slider">
    <label for="comment">Comment</label>
    <textarea cols="53" rows="10" id="comment"></textarea>
</div><!--/#comment-wrap-->

There are no major changes to the plugin, just a few tweaks. If you find anymore bug, please let me know.

View the Demo

116 thoughts on “Sliding Labels v2 – Patch

  1. Pingback: IMAP | Daily Digest for April 8th

  2. Pingback: 45 Fresh Useful JavaScript and jQuery Techniques and Tools « wwwba

  3. I think this is really neat – but I saw some comments elsewhere that this doesn’t help with the ‘space saving’ that inline labels are often used to achieve.

    So I added a tweak to make it ‘Slide and Hide’ – if a field has a value then the slid label is only visible while the field has focus.

    This saves space and keeps the prompt visible while the field is being edited, so *if* space is an issue you may like to use this…

    In the JS file
    - change the onload to ‘.hide’ instead of ‘.animate’ when a field has a value.
    - in the focus – add a ‘.show()’ before a label is animated back (in case it was hidden).
    - in the onblur – add ‘ else label.hide();’ on the check for a label value.

    Thanks for sharing.

  4. Pingback: 40 Useful jQuery Techniques and Plugins - Smashing Magazine

  5. Pingback: 40 Useful jQuery Techniques and Plugins | Web Design Cool

  6. Pingback: 40 Useful jQuery Techniques and Plugins » Shai Perednik.com

  7. Pingback: TG Designer » 40 Useful jQuery Techniques and Plugins

  8. Pingback: 40 Useful jQuery Techniques and Plugins « qeqnes | Designing. jQuery, Ajax, PHP, MySQL and Templates

  9. it looks like the labels are sliding through the walls of the input fields, & this doesn’t look right to me

    imo, the effect would be better if instead of sliding from A to B, the labels faded (ie: faded down & then reappeared at their end position, also with fading)

    pls let me know if the plugin can already accomplish this, as i’ll probably use it

  10. Pingback: 40 Useful jQuery Techniques and Plugins | CMS Code

  11. Pingback:   40 Useful jQuery Techniques and Plugins by webdunyam.net

  12. Pingback: Improve Your Web Forms with Sliding Labels | ChurchCrunch

  13. Pingback: » Plugin JQuery SlidingLabels : pour améliorer l’ergonomie des formulaires - JQuery, PHP, html, design… // arnaud-k : un blog de geek

  14. This is very similar to something I made a few months ago except the label will slide behind the input when the input has text and losses focus.

  15. Pingback: 40 Useful jQuery Techniques and Plugins « Dheerajir's Blog

  16. Pingback: 12 More Creative & Usefull jQuery Plugins « The Creative Project

  17. Pingback: 12 More Creative & Useful jQuery Plugins « The Creative Project

  18. Pingback: Furkan Tunalı v4 beta // 40 Useful jQuery Techniques and Plugins /

  19. Pingback: 40 Useful jQuery Techniques and Plugins | i know idea

  20. Pingback: 40 Useful jQuery Techniques and Plugins via @smashingmag « webbuzz

  21. Pingback: 40 Useful jQuery Techniques and Plugins via @smashingmag | webbuzz

  22. Pingback: 40 Useful jQuery Techniques and Plugins

  23. Pingback: Inspiration

  24. Pingback: ” 40个有用的jQuery插件 – HTML5,CSS3,WEB前端资讯站

  25. Pingback: 40 Useful jQuery Techniques and Plugins « R@j@. R.K

  26. Pingback: 40 herramientas jQuery | Diseño web, accesibilidad, usabilidad, posicionamiento y optimización web - AlmacenPlantillasWeb Blog

  27. Pingback: 45 Fresh Useful JavaScript and jQuery Techniques and Tools | Xiaoqi He ( Harry He ) – Official Blog – 贺孝琦官方博客

  28. Pingback: 45 Fresh Useful JavaScript and jQuery Techniques and Tools « TMHs Web Tips

  29. Pingback: 40 Useful jQuery Techniques and Plugins « TMHs Web Tips