Last active
August 29, 2015 14:09
-
-
Save nicolasrenon/91396fe4cfb69668085e to your computer and use it in GitHub Desktop.
Fallback to handle placeholder for IE 9 and below
This file contains hidden or 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
(function () { | |
'use strict'; | |
var Placeholder = function (element) { | |
this.field = element; | |
this.password = (this.field.attr('type') === 'password'); | |
this.placeholder = this.field.attr('placeholder'); | |
this.init(); | |
}; | |
Placeholder.prototype = { | |
init: function () { | |
this.field.on('blur focus', this.handleEvent.bind(this)); | |
this.field.val(this.placeholder).addClass('placeholder'); | |
if (this.password) { | |
this.field.attr('type', 'text'); | |
} | |
}, | |
handleEvent: function (event) { | |
var value = this.field.val(); | |
if (event.type === 'blur') { | |
this.onBlur(value); | |
} else | |
if (event.type === 'focus') { | |
this.onFocus(value); | |
} else { | |
console.log('Placeholder: event unknown'); | |
} | |
}, | |
onBlur: function (value) { | |
if (value === '') { | |
this.field.val(this.placeholder).addClass('placeholder'); | |
} | |
}, | |
onFocus: function (value) { | |
if (value === this.placeholder) { | |
this.field.val('').removeClass('placeholder'); | |
} | |
if (this.password) { | |
this.field.attr('type', 'password'); | |
} | |
}, | |
destroy: function() { | |
this.field.off('blur focus'); | |
} | |
}; | |
$('input[type="email"], input[type="password"], input[type="text"], textarea').each(function () { | |
new app.Doughnut($(this)); | |
}); | |
})(); |
This file contains hidden or 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
@mixin placeholder { | |
&.placeholder { @content; } | |
&:-moz-placeholder { @content; } | |
&::-moz-placeholder { @content; } | |
&:-ms-input-placeholder { @content; } | |
&::-webkit-input-placeholder { @content; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment