Skip to content

Instantly share code, notes, and snippets.

@yairEO
Created June 16, 2014 12:27
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 yairEO/ee337e46173dcdfd0e78 to your computer and use it in GitHub Desktop.
Save yairEO/ee337e46173dcdfd0e78 to your computer and use it in GitHub Desktop.
fix for placeholders in old IE
(function($){
"use strict";
$.fn.fixPlaceholders = function(){};
if( $.support.placeholders )
return;
var selector = 'input[placeholder], textarea[placeholder]',
originalType;
// custom jQuery methods
$.fn.fixPlaceholders = {
setOriginalType : function(){
if( this.tagName == 'INPUT' )
this.originalType = this.type || 'text';
},
onFocus : function(){
console.log(this.originalType);
if( this.originalType )
this.type = this.originalType;
if( this.value == $(this).attr("placeholder") ){
if( this.tagName == 'INPUT' )
this.value = '';
}
$(this).removeClass('empty');
},
onBlur : function(){
if( this.value == "" ){
if( this.tagName == 'INPUT' )
this.type = 'text';
this.value = $(this).addClass('empty').attr("placeholder");
}
else
$(this).removeClass('empty');
}
}
// bind events
$(document)
.on('focus', selector, $.fn.fixPlaceholders.onFocus)
.on('blur', selector, $.fn.fixPlaceholders.onBlur);
// scan page for inputs
$('input[placeholder]').each(function(){
$.fn.fixPlaceholders.setOriginalType.apply(this);
$.fn.fixPlaceholders.onBlur.apply(this);
})
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment