Skip to content

Instantly share code, notes, and snippets.

@e2goon
Created March 23, 2018 04:29
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 e2goon/a9074d491804c8a6cf1495e94f92d777 to your computer and use it in GitHub Desktop.
Save e2goon/a9074d491804c8a6cf1495e94f92d777 to your computer and use it in GitHub Desktop.
IE8~9 Placeholder
(function IEplaceholder(o) {
var agent = navigator.userAgent.toLowerCase();
var IS_IE = agent.match(/msie\s?(\d{1,2})/g);
if (!IS_IE || IS_IE[0].substr(5, 6) > 9) return;
var $placeholders = $('[placeholder]');
$placeholders
.wrap('<span />')
.each(o.addPlaceholder(o))
.on('keyup', o.keyup)
.parent()
.css(o.styles.wrap)
;
}({
styles: {
wrap: {
position: 'relative',
display: 'block'
},
text: {
position: 'absolute',
left: 0,
top: 0,
right: 0,
bottom: 0
}
},
addPlaceholder: function(o) {
return function() {
var $self = $(this),
$wrap = $self.parent(),
value = $self.attr('placeholder');
$('<span />')
.css(o.styles.text)
.addClass('ie-placeholder')
.text(value)
.appendTo($wrap)
.on('click', function() {
$(this).prev().focus();
})
;
}
},
keyup: function() {
var $placeholder = $(this).next();
if ($(this).val().length) {
$placeholder.hide();
} else {
$placeholder.show();
}
}
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment