Created
June 16, 2014 12:27
-
-
Save yairEO/ee337e46173dcdfd0e78 to your computer and use it in GitHub Desktop.
fix for placeholders in old IE
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
(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