Created
October 30, 2012 03:33
-
-
Save carwin/3978156 to your computer and use it in GitHub Desktop.
Drupal 7: HTML5 placeholder attributes for form items with typical jQuery fallback for older browsers.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function ($) { | |
Drupal.behaviors.yourTheme = { | |
attach: function(context, settings) { | |
$('form label').css('display', 'none'); | |
if(Modernizr.input.placeholder) { | |
// Text inputs | |
var formElements = $('form').find('input, textarea'); | |
$.each(formElements, function(index, value){ | |
if($(this).is('input')){ | |
var label = $(this).siblings('label').text(); | |
}else{ | |
var label = $(this).parent().siblings('label').text(); | |
} | |
$(this).attr('placeholder', label); | |
}); | |
}else{ | |
var formElements = $('form').find('input, textarea'); | |
$.each(formElements, function(index, value) { | |
if($(this).is('input')){ | |
var label = $(this).siblings('label').text(); | |
}else{ | |
var label = $(this).parent().siblings('label').text(); | |
} | |
$(this) | |
.val(label) | |
.focus(function(){ | |
if($(this).val(label)) { | |
$(this).val(''); | |
} | |
}) | |
.blur(function(){ | |
if(!$(this).val()) { | |
$(this).val(label); | |
} | |
}); | |
}); | |
} | |
} | |
} | |
})(jQuery) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this works perfect for text-fields and text-areas. My problem is, that your script also hides labels from checkboxes and radio-buttons, which makes them unusable!