Skip to content

Instantly share code, notes, and snippets.

@makbeta
Last active December 21, 2015 09: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 makbeta/6285404 to your computer and use it in GitHub Desktop.
Save makbeta/6285404 to your computer and use it in GitHub Desktop.
jQuery: show/hide placeholder text in form inputs on hover
$.fn.showHidePlaceholder = function(defaultValue) {
$(this).each(function() {
var $element = $(this);
if($element[0].tagName.toLowerCase() == 'select') {
$selectedOption = $element.children('option:selected');
if($selectedOption.text() == defaultValue || $selectedOption.val() == defaultValue) {
$element.addClass('placeholder');
}
$element.on('change', function() {
$selectedOption = $element.children('option:selected');
if($selectedOption.text() == defaultValue || $selectedOption.val() == defaultValue) {
$element.addClass('placeholder');
} else {
$element.removeClass('placeholder');
}
});
}
else {
if($element.val() == defaultValue) {
$element.addClass('placeholder');
}
$element.on('focus', function() {
if($element.val() == defaultValue) {
$element.val('').removeClass('placeholder');
}
});
$element.on('blur', function() {
if($element.val() == '') {
$element.val(defaultValue).addClass('placeholder');
}
});
}
});
return $(this);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment