Created
February 14, 2013 22:58
-
-
Save greypants/4957179 to your computer and use it in GitHub Desktop.
JS: Placeholder polyfill
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
createPlaceholders: function() { | |
var noPlaceholderSupport = $('html').hasClass('lte9'); | |
if(noPlaceholderSupport) { | |
var createPlaceholder = function() { | |
var self = this; | |
var $this = $(this); | |
var isEmpty = true; | |
var placeholder = this.placeholder; | |
// Wrap in a container | |
$this.wrap('<div class="placeholderWrapper">'); | |
$this.after('<div class="placeholder">' + placeholder + '</div>'); | |
var $placeholder = $this.siblings('.placeholder'); | |
var $wrapper = $this.parent('.placeholderWrapper'); | |
$placeholder.on({ | |
click: function() { | |
$placeholder.hide(); | |
$this.focus(); | |
} | |
}); | |
$this.on({ | |
'click': function(){ | |
$placeholder.hide(); | |
}, | |
'keydown': function() { | |
$placeholder.hide(); | |
}, | |
'blur': function() { | |
if(isEmpty) { | |
$placeholder.show(); | |
} | |
}, | |
change: function() { | |
isEmpty = (self.value.length < 1); | |
} | |
}); | |
}; | |
$('form').find('[placeholder]').each(createPlaceholder); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment