Skip to content

Commit

Permalink
Merge pull request mathiasbynens#54 from sbull/valHooks-only-if-enabled
Browse files Browse the repository at this point in the history
Avoid calling placeholder functions in valHooks unless .placeholder() ha...
  • Loading branch information
mathiasbynens committed Feb 11, 2012
2 parents 4cafa16 + 0558577 commit fefeb21
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions jquery.placeholder.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
'focus.placeholder': clearPlaceholder,
'blur.placeholder': setPlaceholder
})
.data('jquery-placeholder-enabled', true)
.trigger('blur.placeholder').end();
};

Expand All @@ -34,17 +35,23 @@

hooks = {
'get': function(element) {
var $element = $(element);
return $element.hasClass('placeholder') ? '' : element.value;
var $element = $(element),
isPlaceholderEnabled = $element.data('jquery-placeholder-enabled');
return (isPlaceholderEnabled && $element.hasClass('placeholder')) ? '' : element.value;
},
'set': function(element, value) {
var $element = $(element);
if (value == '') {
element.value = value;
// We can’t use `triggerHandler` here because of dummy text/password inputs :(
setPlaceholder.call(element);
} else if ($element.hasClass('placeholder')) {
clearPlaceholder.call(element, true, value) || (element.value = value);
var $element = $(element)
isPlaceholderEnabled = $element.data('jquery-placeholder-enabled');
if (isPlaceholderEnabled) {
if (value == '') {
element.value = value;
// We can’t use `triggerHandler` here because of dummy text/password inputs :(
setPlaceholder.call(element);
} else if ($element.hasClass('placeholder')) {
clearPlaceholder.call(element, true, value) || (element.value = value);
} else {
element.value = value;
}
} else {
element.value = value;
}
Expand Down

0 comments on commit fefeb21

Please sign in to comment.