Skip to content

Instantly share code, notes, and snippets.

@lucasmezencio
Last active May 3, 2018 22:49
Show Gist options
  • Save lucasmezencio/3931326 to your computer and use it in GitHub Desktop.
Save lucasmezencio/3931326 to your computer and use it in GitHub Desktop.
jQuery 'plugin' dor defaultvalue of an input
;(function($) {
$.fn.defaultValue = function() {
$(this).each(function(i, el) {
var actualValue,
$el = $(this),
defaultValue = el.value;
$el.on('focus', function() {
actualValue = $el.val();
if (actualValue == defaultValue) {
$el.val('');
}
});
$el.on('blur', function() {
actualValue = $el.val();
if (!actualValue) {
$el.val(defaultValue);
}
});
});
}
})(jQuery);
@gabeidx
Copy link

gabeidx commented Oct 23, 2012

Vc tá ligado que existe o atributo placeholder que faz exatamente isso né?

@lucasmezencio
Copy link
Author

Opa, só vi seu comentário agora.
Sim, tô ligado, mas ele não funciona em todos os browsers (infelizmente).
=]

@lucasmezencio
Copy link
Author

@ionurboz
Copy link

ionurboz commented May 3, 2018

thnx

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