Skip to content

Instantly share code, notes, and snippets.

@ferronrsmith
Last active December 17, 2015 14:19
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 ferronrsmith/5623455 to your computer and use it in GitHub Desktop.
Save ferronrsmith/5623455 to your computer and use it in GitHub Desktop.
The following functionality is a custom-based poly-fill placeholder for AngularJS. For browsers lower than IE 10 the in-built placeholder functionality is used, otherwise the poly-fill is used
/***
* The following functionality is a custom-based poly-fill placeholder for AngularJS
* @example <input id="weight" name="weight" type="number" defaulttext="enter your weight"
* min="50" max="500" required />
* For browsers lower than IE 10 the in-built placeholder functionality is used, otherwise
* the poly-fill is used
*/
app.directive('defaulttext', function($timeout){
if (!$.browser.msie || $.browser.version >= 10) {
return {
link: function(scope, elm, attrs){
$timeout(function(){
$(elm).attr('placeholder', attrs.defaulttext);
});
}
};
}
return {
link: function(scope, elm, attrs){
if (attrs.type === 'password') {
return;
}
$timeout(function(){
$(elm).val(attrs.defaulttext).focus(function(){
if ($(this).val() == $(this).attr('defaulttext')) {
$(this).val('');
}
}).blur(function(){
if ($(this).val() == '') {
$(this).val($(this).attr('defaulttext'));
}
});
});
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment