Skip to content

Instantly share code, notes, and snippets.

@veeracs
Created June 13, 2013 02:23
Show Gist options
  • Save veeracs/5770791 to your computer and use it in GitHub Desktop.
Save veeracs/5770791 to your computer and use it in GitHub Desktop.
/*
HTML5 placeholder polyfill for IE8, 9
*/
angular.module('directives.placeholder', []).directive('placeholder', function(){
// cn-placeholder directive definition object
return {
restrict: 'A',
require: 'ngModel',
link: function(scope, elm, attrs, ctrl) {
if (!Modernizr.placeholder) {
// setup the label overlay for input
var label = elm.parent()
.css({'position':'relative'})
.prepend('<span>' + elm.attr('placeholder') + '</span>')
.find('span')
.css({'color':'#999', 'position':'absolute','top':'5px','left':'7px'})
.bind('click', function(){
elm[0].focus();
});
// remove the label overlay when a value is typed
elm.bind('keyup keydown', function(){
if (elm.val() !== '') {
label.remove();
} else {
elm.parent().prepend(label);
}
});
}
}
};
});
@veeracs
Copy link
Author

veeracs commented May 12, 2014

Thank you for your comments and reference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment