-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make .placeholder classname configurable #39
Closed
Closed
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1f9ea0e
Adds custom placeholder className option.
cheeaun cfb623f
Fixes silly scoping and uses $.fn.placeholder.className for config.
cheeaun d396b51
Use $.fn.placeholder.className wherever possible.
cheeaun 79bf24d
Only bind domready and unload event (once) when placeholder is called.
cheeaun 5e9201f
Assume the dom is ready by the time .placeholder is called.
cheeaun d35af81
Move and cache some variables.
cheeaun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,8 @@ | |
;(function(window, document, $) { | ||
|
||
var isInputSupported = 'placeholder' in document.createElement('input'), | ||
isTextareaSupported = 'placeholder' in document.createElement('textarea'); | ||
isTextareaSupported = 'placeholder' in document.createElement('textarea'), | ||
placeholderClassName = 'placeholder'; | ||
|
||
if (isInputSupported && isTextareaSupported) { | ||
|
||
|
@@ -15,6 +16,7 @@ | |
} else { | ||
|
||
$.fn.placeholder = function() { | ||
placeholderClassName = $.fn.placeholder.className || placeholderClassName; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, it’s not needed/used in this scope, so I guess you can remove this line. |
||
return this.filter((isInputSupported ? 'textarea' : ':input') + '[placeholder]') | ||
.bind('focus.placeholder', clearPlaceholder) | ||
.bind('blur.placeholder', setPlaceholder) | ||
|
@@ -28,7 +30,7 @@ | |
// Look for forms | ||
$('form').bind('submit.placeholder', function() { | ||
// Clear the placeholder values so they don’t get submitted | ||
var $inputs = $('.placeholder', this).each(clearPlaceholder); | ||
var $inputs = $('.' + placeholderClassName, this).each(clearPlaceholder); | ||
setTimeout(function() { | ||
$inputs.each(setPlaceholder); | ||
}, 10); | ||
|
@@ -37,7 +39,7 @@ | |
|
||
// Clear placeholder values upon page reload | ||
$(window).bind('unload.placeholder', function() { | ||
$('.placeholder').val(''); | ||
$('.' + placeholderClassName).val(''); | ||
}); | ||
|
||
} | ||
|
@@ -56,11 +58,11 @@ | |
|
||
function clearPlaceholder() { | ||
var $input = $(this); | ||
if ($input.val() === $input.attr('placeholder') && $input.hasClass('placeholder')) { | ||
if ($input.val() === $input.attr('placeholder') && $input.hasClass(placeholderClassName)) { | ||
if ($input.data('placeholder-password')) { | ||
$input.hide().next().show().focus().attr('id', $input.removeAttr('id').data('placeholder-id')); | ||
} else { | ||
$input.val('').removeClass('placeholder'); | ||
$input.val('').removeClass(placeholderClassName); | ||
} | ||
} | ||
} | ||
|
@@ -91,9 +93,9 @@ | |
} | ||
$input = $input.removeAttr('id').hide().prev().attr('id', id).show(); | ||
} | ||
$input.addClass('placeholder').val($input.attr('placeholder')); | ||
$input.addClass(placeholderClassName).val($input.attr('placeholder')); | ||
} else { | ||
$input.removeClass('placeholder'); | ||
$input.removeClass(placeholderClassName); | ||
} | ||
} | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line isn’t needed, is it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's used in
clearPlaceholder
andsetPlaceholder
functions, plus thedomready
andunload
event handlers.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What I was thinking is that this should be added to the code after
$.fn.placeholder
is declared:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I see, so I should only get the className via
$.fn.placeholder.className
instead of a variable (outside of the function scopes)?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, and cache/re-use the
$.fn.placeholder.className
wherever possible (per scope).We should probably move the
$(…)
and the$(window).bind(…)
code inside of the$.fn.placeholder = fn
block, while still making sure these only get called/bound once (even when$().placeholder()
is used multiple times). We could use.one()
instead of.bind()
, but what about the$(…)
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I use
.one()
for theready
event.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you tested this and does it work?
This doesn’t seem to work: http://jsfiddle.net/mathias/tMDrZ/ Any ideas? Perhaps we could use something like this: http://jsfiddle.net/tMDrZ/1/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, it doesn't work. I thought about it, and ultimately we can assume that by the time
.placeholder
is called,domready
is already fired.