Skip to content

Instantly share code, notes, and snippets.

@thomseddon
Forked from nobuf/gist:3419910
Last active December 27, 2016 02:58
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save thomseddon/4703810 to your computer and use it in GitHub Desktop.
Save thomseddon/4703810 to your computer and use it in GitHub Desktop.
Supporting placeholder on IE9 with AngularJS (without jQuery) > Removed jQuery dependency > Slight optimisation in retrieving placeholder text
angular.module('test', [])
.directive('placeholder', function($timeout){
var i = document.createElement('input');
if ('placeholder' in i) {
return {}
}
return {
link: function(scope, elm, attrs){
if (attrs.type === 'password') {
return;
}
$timeout(function(){
elm.val(attrs.placeholder);
elm.bind('focus', function(){
if (elm.val() == attrs.placeholder) {
elm.val('');
}
}).bind('blur', function(){
if (elm.val() == '') {
elm.val(attrs.placeholder);
}
});
});
}
}
});
@danbarua
Copy link

Great, thanks!

@kkurni
Copy link

kkurni commented Oct 17, 2013

This doesn't work with required attributes.

I tried this but it will ignore the required validation

@wlingke
Copy link

wlingke commented Oct 22, 2013

Issue is it puts a value on the element so if a user ignores a field, it submits the value of the placeholder

@johnnie021
Copy link

I appreciate this. Thanks!

@SnehaSuryawanshi
Copy link

Thank you :) Works well in IE8 & IE9!

@lutsifer
Copy link

lutsifer commented Apr 9, 2016

Great, thanks:)

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