Last active
December 16, 2015 09:29
-
-
Save ingowennemaring/5413197 to your computer and use it in GitHub Desktop.
Polyfill for missing placeholder feature in IE <= 9
This file contains 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
/* | |
Placeholder für MSIE/Opera | |
@param mode => only needed for e.g. responsive sites, | |
if sometimes placeholders should be shown and sometimes not or the placeholder changed in runtime | |
mode === 'hide' => add behavior, events and so on, but show no placeholder right now | |
mode === 'update' => update placeholders which were updated in runtime | |
*/ | |
var placeholder = function( hidePlaceholder ) { | |
jQuery.support.placeholder = false; | |
var test = document.createElement('input'); | |
if('placeholder' in test) jQuery.support.placeholder = true; | |
if(!$.support.placeholder) { | |
var active = document.activeElement, | |
input = $('input[placeholder]'); | |
input | |
.each( | |
function() { | |
var me = $(this); | |
if( me.attr('type') === 'password' ) me.data('password', 'true'); | |
if( mode === 'update' && me.hasClass('hasPlaceholder') ) { | |
me.val( me.attr('placeholder') ); | |
} | |
} | |
) | |
.on( | |
'focus', | |
function() { | |
var me = $(this); | |
if( me.data('password') ) me.attr('type', 'password'); | |
if (me.attr('placeholder') !== '' && me.val() === me.attr('placeholder')) { | |
me.val('').removeClass('hasPlaceholder'); | |
} | |
} | |
) | |
.on( | |
'blur', | |
function() { | |
var me = $(this); | |
if(me.attr('placeholder') !== '' && (me.val() === '' || me.val() === me.attr('placeholder'))) { | |
me.val(me.attr('placeholder')).addClass('hasPlaceholder'); | |
if( me.data('password') ) me.attr('type', 'text'); | |
} | |
} | |
) | |
.blur(); | |
$(active).focus(); | |
$('form').on( | |
'submit', | |
function() { | |
var me = $(this); | |
me.find('.hasPlaceholder').each( function() { me.val(''); } ); | |
} | |
); | |
if( mode === 'hide' ) { | |
input.each( | |
function() { | |
var me = $(this); | |
if( me.val() === me.attr('placeholder') ) me.val('').removeClass('hasPlaceholder'); | |
} | |
); | |
} | |
} //if(!$.support.placeholder) | |
}; | |
placeholder(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment