Created
August 21, 2012 22:15
-
-
Save nobuf/3419910 to your computer and use it in GitHub Desktop.
Supporting placeholder on IE9 with AngularJS + jQuery
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
angular.module('test', []) | |
.directive('placeholder', function($timeout){ | |
if (!$.browser.msie || $.browser.version >= 10) { | |
return {}; | |
} | |
return { | |
link: function(scope, elm, attrs){ | |
if (attrs.type === 'password') { | |
return; | |
} | |
$timeout(function(){ | |
elm.val(attrs.placeholder).focus(function(){ | |
if ($(this).val() == $(this).attr('placeholder')) { | |
$(this).val(''); | |
} | |
}).blur(function(){ | |
if ($(this).val() == '') { | |
$(this).val($(this).attr('placeholder')); | |
} | |
}); | |
}); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
you should consider using this statement for placeholder detection rather than browser sniffing.
if ('placeholder' in $('<input type="text">')[0] == false)
Also you gist will leak memonry as you are not cleaning up the events