Skip to content

Commit

Permalink
Don’t focus empty inputs on submit to allow for optional fields with …
Browse files Browse the repository at this point in the history
…placeholders. Fixes issue mathiasbynens#2
  • Loading branch information
Mathias Bynens authored and Mathias Bynens committed Jul 2, 2010
1 parent 67dade0 commit 0a1f1ca
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
20 changes: 10 additions & 10 deletions jquery.placeholder.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* HTML5 Placeholder jQuery Plugin
* HTML5 Placeholder jQuery Plugin v1.0
* @link http://github.com/mathiasbynens/Placeholder-jQuery-Plugin
* @author Mathias Bynens <http://mathiasbynens.be/>
*/
Expand All @@ -20,17 +20,17 @@
$elem.removeClass('placeholder');
};
};
// Submit handler on all forms containing input[placeholder]
// Needs to be via .click()
$('form:has([placeholder]) :submit').click(function() {
// if ($(this).has('.placeholder') never returns false here — WTF?
if ($(this.form).find('.placeholder').length) {
$(this.form).find('.placeholder:first').val('').focus().removeClass('placeholder');
return false;
};
// This selector could be shortened to form:has([placeholder]) but that would be less efficient
$('form:has(input[placeholder])').submit(function() {
// Clear the placeholder values so they don’t get submitted
$('.placeholder', this).val('');
});
// Clear placeholder values upon page reload
$(window).unload(function() {
$('.placeholder').val('');
});
// Yes, .each() — in case .placeholder() is called on several elements, which is very likely, e.g. $('input').placeholder();
return $(this).each(function() {
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')) {
Expand Down
4 changes: 2 additions & 2 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 0a1f1ca

Please sign in to comment.