Skip to content
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
wants to merge 6 commits into from
Closed
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 26 additions & 22 deletions jquery.placeholder.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,33 @@
} else {

$.fn.placeholder = function() {

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cache var className = $.fn.placeholder.className; here please (since it’s used twice in this scope).

$().one('ready', function() {
// Look for forms
$('form').bind('submit.placeholder', function() {
// Clear the placeholder values so they don’t get submitted
var $inputs = $('.' + $.fn.placeholder.className, this).each(clearPlaceholder);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

…so you can use it here…

setTimeout(function() {
$inputs.each(setPlaceholder);
}, 10);
});
});

// Clear placeholder values upon page reload
$(window).one('unload.placeholder', function() {
$('.' + $.fn.placeholder.className).val('');
});
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could chain these together: $(window).ready(…).one(…);


return this.filter((isInputSupported ? 'textarea' : ':input') + '[placeholder]')
.bind('focus.placeholder', clearPlaceholder)
.bind('blur.placeholder', setPlaceholder)
.trigger('blur.placeholder').end();
};

$.fn.placeholder.className = 'placeholder';
$.fn.placeholder.input = isInputSupported;
$.fn.placeholder.textarea = isTextareaSupported;

$(function() {
// Look for forms
$('form').bind('submit.placeholder', function() {
// Clear the placeholder values so they don’t get submitted
var $inputs = $('.placeholder', this).each(clearPlaceholder);
setTimeout(function() {
$inputs.each(setPlaceholder);
}, 10);
});
});

// Clear placeholder values upon page reload
$(window).bind('unload.placeholder', function() {
$('.placeholder').val('');
});

}

function args(elem) {
Expand All @@ -55,12 +57,13 @@
}

function clearPlaceholder() {
var $input = $(this);
if ($input.val() === $input.attr('placeholder') && $input.hasClass('placeholder')) {
var $input = $(this),
placeholderClassName = $.fn.placeholder.className;
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);
}
}
}
Expand All @@ -69,7 +72,8 @@
var $replacement,
$input = $(this),
$origInput = $input,
id = this.id;
id = this.id,
placeholderClassName = $.fn.placeholder.className;
if ($input.val() === '') {
if ($input.is(':password')) {
if (!$input.data('placeholder-textinput')) {
Expand All @@ -91,9 +95,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);
}
}

Expand Down