Created
October 24, 2014 10:59
-
-
Save rudiedirkx/a89bd54102d40be9be69 to your computer and use it in GitHub Desktop.
Drupal placeholder polyfill
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.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