CSSKarma

display your <style>

designing the web since 2002

Sliding Labels v2 – Patch

banner

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

Tags: , , ,

You can leave a response, or trackback from your own site.

« Older Comments |

Comments (42)

  1. Matt says:

    Thanks for making these changes. I was struggling with the textarea option.

  2. Bonita says:

    Thank you for this absolutely awesome form script thingy!! One question though – how do I style the textarea box? I have added the following in the CSS file, but this doesn’t seem to be correct..?

    input[type="textarea"] { width:300px; border:1px solid #999; padding:5px; -moz-border-radius:4px; background:#272c2e;}
    input[type="textarea"]:focus { border-color:#bed73d; }

    Please help! **QQ** :D

  3. Bonita says:

    ooooh wait I got it!! I’m such a noodle!! lol!! :P

    textarea[name="comment"] { width:300px; border:1px solid #999; padding:5px; -moz-border-radius:4px; background:#272c2e;}
    textarea[name="comment"]:focus { border-color:#bed73d; }

  4. Jason says:

    If you have a small input field and the label takes up the whole box, you need to add this to the top of the plugin to get the label to slide out of the way when a user clicks on it

    $(‘.slider label’).click(function(){ $(this).next().focus(); });

  5. Dan B. says:

    You mentioned the labels have the potential to slide upward?
    Could you show me how to change the .js to achieve this?

    Thanks in advance!

  6. burn says:

    may i join Dan B. and ask for enlightenment re slide upward
    thanks in advance

  7. animhotep says:

    the next version will work with select?

  8. animhotep says:

    yappeyy, it’s only need to add select $(‘input, textarea, select’).focus(function(){…

  9. Jesse s says:

    DOOOOPE.cool and unique. thanks for sharing

  10. Chad says:

    I can’t seem to get the script to work. I created a new php file. Called to the latest version of jquery.js and input the slider javascript code into the head section in a script call. Pasted the comment wrap div that OP provided. What am I doing wrong here?

  11. Rilwis says:

    This effect is nice. I love this. Thank you for that.

  12. Tom says:

    Great. But it doesn’t work with this form structure :

  13. Nice simple and clean. Thanks!

    btw, (I know it should) but does anyone know if tihs plays nice with the Validate plugin?

  14. ilz says:

    That’s sick! Nice subtle effect.. a lot more user friendly than making people erase the text in the inputs before they type.

  15. Really, really great. Such an elegant solution.

  16. great idea!

    but, shouldn’t the .focus(…) and .blur(…) part of the code be outside the .each(…)?

  17. Cody says:

    anyone figure out how to move the text field boxes to the left? There seems to always be a lot of padding on the left. Anyone??

  18. Anthony says:

    haha, I too missed the “.slider{ position:relative; } in the CSS” at first.
    Thanks for this!
    Very cool!

  19. sc says:

    do you have select boxes that work ?

  20. Karen says:

    unique script, very well done.

  21. Jitendra vyas says:

    Is this screen reader compatible?

  22. David says:

    Hey,
    I use your sliding labels for a commentformular on these page: http://www.mmindgame.de but if i activate the lightbox2.0 plugin for wordpress it doesnt work anymore. I dont know the problem. Can someone help me?

  23. Vinicius says:

    Hi,
    I liked it very much and I made a modification to labels go to top instead of left.
    Could you parameterize the code to go to the left or to top?
    Thanks a lot!

  24. Paul says:

    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.

  25. david says:

    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

  26. Seamus says:

    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.

  27. James R. says:

    Very nice! I think I am going to have to add this to the next site I do!

    Thanks!

|

Trackbacks & Pingbacks (73)

Leave a Reply

New from the blog

Are My Sites Up? authenticjobs.com