Skip to content

Commit b3c33a6

Browse files
committed
Added custom css class support
1 parent 5e289b7 commit b3c33a6

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ Use the plugin as follows:
2626
$('input, textarea').placeholder();
2727
```
2828

29+
By default, `.placeholder` css class will be added. You can override default by passing the `customClass` option:
30+
```js
31+
$('input, textarea').placeholder({customClass: 'my-placeholder'});
32+
```
33+
2934
You’ll still be able to use `jQuery#val()` to get and set the input values. If the element is currently showing a placeholder, `.val()` will return an empty string instead of the placeholder text, just like it does in browsers with a native `@placeholder` implementation. Calling `.val('')` to set an element’s value to the empty string will result in the placeholder text (re)appearing.
3035

3136
### CSS

index.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
label code { cursor: pointer; float: left; width: 150px; }
1010
input { width: 14em; }
1111
textarea { height: 5em; width: 20em; }
12-
.placeholder { color: #aaa; }
12+
.my-placeholder { color: #aaa; }
1313
.note { border: 1px solid orange; padding: 1em; background: #ffffe0; }
1414
/* #p { background: lime; } */
1515
</style>
@@ -37,7 +37,7 @@ <h1>HTML5 Placeholder jQuery Plugin</h1>
3737
// Then uncomment the last rule in the <style> element (in the <head>).
3838
$(function() {
3939
// Invoke the plugin
40-
$('input, textarea').placeholder();
40+
$('input, textarea').placeholder({customClass:'my-placeholder'});
4141
// That’s it, really.
4242
// Now display a message if the browser supports placeholder natively
4343
var html;

jquery.placeholder.js

+15-9
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,17 @@
2828

2929
} else {
3030

31-
placeholder = $.fn.placeholder = function() {
31+
var settings = {};
32+
33+
placeholder = $.fn.placeholder = function(options) {
34+
35+
var defaults = {customClass: 'placeholder'};
36+
settings = $.extend({}, defaults, options);
37+
3238
var $this = this;
3339
$this
3440
.filter((isInputSupported ? 'textarea' : ':input') + '[placeholder]')
35-
.not('.placeholder')
41+
.not('.'+settings.customClass)
3642
.bind({
3743
'focus.placeholder': clearPlaceholder,
3844
'blur.placeholder': setPlaceholder
@@ -74,7 +80,7 @@
7480
// We can't use `triggerHandler` here because of dummy text/password inputs :(
7581
setPlaceholder.call(element);
7682
}
77-
} else if ($element.hasClass('placeholder')) {
83+
} else if ($element.hasClass(settings.customClass)) {
7884
clearPlaceholder.call(element, true, value) || (element.value = value);
7985
} else {
8086
element.value = value;
@@ -97,7 +103,7 @@
97103
// Look for forms
98104
$(document).delegate('form', 'submit.placeholder', function() {
99105
// Clear the placeholder values so they don't get submitted
100-
var $inputs = $('.placeholder', this).each(clearPlaceholder);
106+
var $inputs = $('.'+settings.customClass, this).each(clearPlaceholder);
101107
setTimeout(function() {
102108
$inputs.each(setPlaceholder);
103109
}, 10);
@@ -106,7 +112,7 @@
106112

107113
// Clear placeholder values upon page reload
108114
$(window).bind('beforeunload.placeholder', function() {
109-
$('.placeholder').each(function() {
115+
$('.'+settings.customClass).each(function() {
110116
this.value = '';
111117
});
112118
});
@@ -128,7 +134,7 @@
128134
function clearPlaceholder(event, value) {
129135
var input = this;
130136
var $input = $(input);
131-
if (input.value == $input.attr('placeholder') && $input.hasClass('placeholder')) {
137+
if (input.value == $input.attr('placeholder') && $input.hasClass(settings.customClass)) {
132138
if ($input.data('placeholder-password')) {
133139
$input = $input.hide().nextAll('input[type="password"]:first').show().attr('id', $input.removeAttr('id').data('placeholder-id'));
134140
// If `clearPlaceholder` was called from `$.valHooks.input.set`
@@ -138,7 +144,7 @@
138144
$input.focus();
139145
} else {
140146
input.value = '';
141-
$input.removeClass('placeholder');
147+
$input.removeClass(settings.customClass);
142148
input == safeActiveElement() && input.select();
143149
}
144150
}
@@ -174,10 +180,10 @@
174180
$input = $input.removeAttr('id').hide().prevAll('input[type="text"]:first').attr('id', id).show();
175181
// Note: `$input[0] != input` now!
176182
}
177-
$input.addClass('placeholder');
183+
$input.addClass(settings.customClass);
178184
$input[0].value = $input.attr('placeholder');
179185
} else {
180-
$input.removeClass('placeholder');
186+
$input.removeClass(settings.customClass);
181187
}
182188
}
183189

0 commit comments

Comments
 (0)