Skip to content

Instantly share code, notes, and snippets.

@rudiedirkx
Created October 24, 2014 10:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rudiedirkx/a89bd54102d40be9be69 to your computer and use it in GitHub Desktop.
Save rudiedirkx/a89bd54102d40be9be69 to your computer and use it in GitHub Desktop.
Drupal placeholder polyfill
(function ($) {
Drupal.behaviors.placeholderScripts = {
attach: function(context, settings) {
/**
* Placeholder support
*/
if ( !('placeholder' in document.createElement('input')) ) {
function onFocus(e) {
this.$this || (this.$this = $(this));
if ( this.$this.val() == this.$this.attr('placeholder') ) {
this.$this.val('');
this.$this.removeClass('showing-placeholder');
}
}
function onBlur(e) {
this.$this || (this.$this = $(this));
if ( this.$this.val() == '' ) {
this.$this.val(this.$this.attr('placeholder'));
this.$this.addClass('showing-placeholder');
}
}
$('input[placeholder], textarea[placeholder]')
.bind('focus', onFocus)
.bind('blur', onBlur)
.each(function(i, el) {
onBlur.call(el);
if ( el.form && !el.form.$placeholdered ) {
el.form.$placeholdered = [el];
var onSubmit = function(e) {
$.each(this.$placeholdered || this.form.$placeholdered, function(i, el) {
onFocus.call(el);
});
// @TODO Is there a better way to be the very last?
setTimeout(function() {
$.each($form[0].$placeholdered, function(i, el) {
onBlur.call(el);
});
}, 33);
};
var $form = $(el.form);
$form.bind('submit', onSubmit);
$form.find('input[type="submit"]').bind('mousedown', onSubmit);
var listeners = $form.data('events').submit;
listeners.unshift(listeners.pop());
}
else if ( el.form ) {
el.form.$placeholdered.push(el);
}
});
}
}
}
}(jQuery));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment