Skip to content

Instantly share code, notes, and snippets.

@hagenburger
Created April 26, 2010 17:15
Show Gist options
  • Save hagenburger/379601 to your computer and use it in GitHub Desktop.
Save hagenburger/379601 to your computer and use it in GitHub Desktop.
// Released under MIT license: http://www.opensource.org/licenses/mit-license.php
$('[placeholder]').focus(function() {
var input = $(this);
if (input.val() == input.attr('placeholder')) {
input.val('');
input.removeClass('placeholder');
}
}).blur(function() {
var input = $(this);
if (input.val() == '' || input.val() == input.attr('placeholder')) {
input.addClass('placeholder');
input.val(input.attr('placeholder'));
}
}).blur().parents('form').submit(function() {
$(this).find('[placeholder]').each(function() {
var input = $(this);
if (input.val() == input.attr('placeholder')) {
input.val('');
}
})
});
@smxi
Copy link

smxi commented Jan 12, 2016

Just as an aside, this would NEVER have worked as expected, because the original code never loads the contents of the placeholder on page load.

I'll fork this with correct code, and link to that, rather than adding more confusion to this page.

Again, I would recommend against using the top code as is because it's incomplete and technically wrong, but the core logic is fine, just incomplete and inadequate to most standard use cases. It looks like it wasn't actually tested on the browsers it was targeted at since the issues would have been obvious immediately.

@hzuhyb
Copy link

hzuhyb commented May 19, 2016

This is imperfect way. please refer https://github.com/mathiasbynens/jquery-placeholder.

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