Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiasbynens committed Jan 19, 2010
0 parents commit b230407
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* crlf=input
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# HTML5 Placeholder jQuery Plugin

This plugin was based on a code snippet by [Paul Irish](http://paulirish.com/), featured in [jQuery 1.4 Hawtness #1](http://jquery14.com/day-05/jquery-1-4-hawtness-1-with-paul-irish). I added some functionality, did some optimizations here and there, and made a plugin out of it.

## Demo & Examples

[http://mathiasbynens.be/examples/placeholder](http://mathiasbynens.be/examples/placeholder)

## Example Usage

### HTML

<input type="text" name="foo" placeholder="Enter some text here">
<input type="email" name="bar" placeholder="Enter your email address">
<input type="search" name="baz" placeholder="Search this site…">
<input type="url" name="wtf" placeholder="Enter your URL">
<input type="tel" name="omg" placeholder="Enter your phone number">

### jQuery

$('input').placeholder();

## Notes

The plugin automatically checks if the browser supports the HTML5 placeholder attribute for inputs natively. If this is the case, the plugin won’t do anything.
The plugin will ignore password inputs.

## Credits

Kudos to [Paul Irish](http://paulirish.com/) for his snippet and everyone from [#jquery](http://webchat.freenode.net/?channels=jquery) for the tips and ideas.

_[Mathias](http://mathiasbynens.be/)_
54 changes: 54 additions & 0 deletions jquery.placeholder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*!
* HTML5 Placeholder jQuery Plugin
* @link http://github.com/mathiasbynens/Placeholder-jQuery-Plugin
* @author Mathias Bynens <http://mathiasbynens.be/>
*/
;(function($) {
$.fn.placeholder = function() {
// Quit if there’s support for HTML5 placeholder
if (this[0] && 'placeholder' in document.createElement('input')) {
// Allow chaining
return this;
};
// Made this a function, because we actually need it on two different occasions:
// 1) Once when the DOM is loaded;
// 2) Once every time the focusout() is triggered.
function setPlaceholder($elem) {
if ($elem.val() === '') {
$elem.val($elem.attr('placeholder'));
};
};
function preventSubmit($elem) {
if ($elem.val() === $elem.attr('placeholder')) {
$elem.val('').focus();
return false;
};
};
// Yes, .each() — in case .placeholder() is called on several elements, which is very likely, e.g. $('input').placeholder();
return $(this).each(function() {
var $input = $(this);
// Quit if the current element is a password input, or not an input at all
if ($input.is(':password') || !$input.is(':input')) {
return;
}
setPlaceholder($input);
// Cancel both the submit event of this form and the click event of the submit button of this form
// I found this to be necessary when using the jQuery Validation Plugin
// Even preventDefault() failed — AMIDOINITRITE?!
$(this.form).submit(function() {
// preventDefault(); doesn’t seem to work here
preventSubmit($input);
}).find('input[type=submit]').click(function() {
// preventDefault(); doesn’t seem to work here
preventSubmit($input);
});
$input.focusin(function() {
if ($input.val() === $input.attr('placeholder')) {
$input.val('');
};
}).focusout(function() {
setPlaceholder($input);
});
});
};
})(jQuery);
6 changes: 6 additions & 0 deletions jquery.placeholder.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b230407

Please sign in to comment.