Skip to content

Instantly share code, notes, and snippets.

@pounard
Created October 30, 2015 09:31
Show Gist options
  • Save pounard/b511b16f4e46fe8c5cdd to your computer and use it in GitHub Desktop.
Save pounard/b511b16f4e46fe8c5cdd to your computer and use it in GitHub Desktop.
Not a polyfill, but does the trick for graceful degradation without complex and buggy JavaScript code.
/*jslint browser: true, plusplus: true, todo: true, white: true */
/*globals Drupal, jQuery */
// Simple placeholder fix for IE8, not a polyfill:
// If a field has no label displayed:
// - If the label is hidden (sr-only or element-invisible) then just show it.
// - If there is no label but a placeholder, build a label using the
// placeholder value.
(function(jQuery) {
"use strict";
Drupal.behaviors.ie8placeholder = {
attach: function (context) {
var jContext = jQuery(context);
jContext.find('[placeholder]').once('ie8placeholder').each(function () {
var label;
if (this.id) {
label = jContext.find('label[for=' + this.id + ']');
if (label.length) {
// Element has a label
label.removeClass('element-invisible').removeClass('sr-only').show();
} else {
// Element has no label
jQuery(this).before('<label for="' + this.id + '">' + this.placeholder + '</label>');
}
}
});
}
};
}(jQuery));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment