Skip to content

Instantly share code, notes, and snippets.

@riddle
Created August 3, 2010 14:26
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save riddle/506462 to your computer and use it in GitHub Desktop.
Save riddle/506462 to your computer and use it in GitHub Desktop.
html5placeholder.js
enablePlaceholders: function() {
if (!('placeholder' in document.createElement('input'))) {
// enable <input placeholder> for browsers not drinking HTML5 kool-aid
$('input[placeholder]').each(function() {
var $this = $(this);
var placeholder = $this.attr('placeholder');
var insert_placeholder = function() {
if ($this.val() === '') {
$this.val(placeholder);
$this.addClass('placeholder');
}
};
var remove_placeholder = function() {
if ($this.val() === placeholder) {
$this.val('');
$this.removeClass('placeholder');
}
};
$this.bind('focus', remove_placeholder);
$this.bind('blur', insert_placeholder);
// https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Objects/Function/Apply
insert_placeholder.apply(this);
// prevent submission with default placeholder
$this.parents('form').bind('submit', function() {
remove_placeholder();
});
});
}
}
@mathiasbynens
Copy link

You could also choose to use my placeholder plugin, which has the added advantage of working for textareas. The only thing I didn’t add support for (yet?) is password inputs.

@riddle
Copy link
Author

riddle commented Aug 3, 2010

I think plugins are a neat idea in general, but any plugin wants to cover edge cases (like textarea or password input in this case). This snippet doesn’t work that way and I don’t think it should. :)

@patrys
Copy link

patrys commented Aug 3, 2010

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