Created
June 13, 2013 02:23
-
-
Save veeracs/5770791 to your computer and use it in GitHub Desktop.
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
/* | |
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); | |
} | |
}); | |
} | |
} | |
}; | |
}); |
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
The following cases are not handled in this directive:
Handled those here. https://github.com/vikasgulati/angularjs-placeholder.