-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
* Use `jQuery.valHooks` to make sure `.val()` and `.val(value)` work as expected. Avoid using `.val()` internally. This fixes #30. * Tweak the syntax a bit here and there now that we’ve dropped support for pre-1.6 jQueries.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
/*! http://mths.be/placeholder v1.8.7 by @mathias */ | ||
/*! http://mths.be/placeholder v2.0.0 by @mathias */ | ||
;(function(window, document, $) { | ||
|
||
var isInputSupported = 'placeholder' in document.createElement('input'), | ||
isTextareaSupported = 'placeholder' in document.createElement('textarea'), | ||
prototype = $.fn, | ||
valHooks = $.valHooks, | ||
hooks, | ||
placeholder; | ||
|
||
if (isInputSupported && isTextareaSupported) { | ||
|
@@ -20,14 +22,40 @@ | |
return this | ||
.filter((isInputSupported ? 'textarea' : ':input') + '[placeholder]') | ||
.not('.placeholder') | ||
.bind('focus.placeholder', clearPlaceholder) | ||
.bind('blur.placeholder', setPlaceholder) | ||
.bind({ | ||
'focus.placeholder': clearPlaceholder, | ||
'blur.placeholder': setPlaceholder | ||
}) | ||
.trigger('blur.placeholder').end(); | ||
}; | ||
|
||
placeholder.input = isInputSupported; | ||
placeholder.textarea = isTextareaSupported; | ||
|
||
hooks = { | ||
'get': function(element) { | ||
var $element = $(element); | ||
return $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); | ||
} else { | ||
element.value = value; | ||
} | ||
// `set` can not return `undefined`; see http://jsapi.info/jquery/1.7.1/val#L2363 | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
mathiasbynens
Author
Owner
|
||
return $element; | ||
} | ||
}; | ||
|
||
isInputSupported || (valHooks.input = hooks); | ||
This comment has been minimized.
Sorry, something went wrong.
devongovett
|
||
isTextareaSupported || (valHooks.textarea = hooks); | ||
|
||
$(function() { | ||
// Look for forms | ||
$(document).delegate('form', 'submit.placeholder', function() { | ||
|
@@ -41,7 +69,9 @@ | |
|
||
// Clear placeholder values upon page reload | ||
$(window).bind('unload.placeholder', function() { | ||
$('.placeholder').val(''); | ||
$('.placeholder').each(function() { | ||
this.value = ''; | ||
}); | ||
}); | ||
|
||
} | ||
|
@@ -58,24 +88,32 @@ | |
return newAttrs; | ||
} | ||
|
||
function clearPlaceholder() { | ||
var $input = $(this); | ||
if ($input.val() === $input.attr('placeholder') && $input.hasClass('placeholder')) { | ||
function clearPlaceholder(event, value) { | ||
var input = this, | ||
$input = $(input); | ||
if (input.value == $input.attr('placeholder') && $input.hasClass('placeholder')) { | ||
if ($input.data('placeholder-password')) { | ||
$input.hide().next().show().focus().attr('id', $input.removeAttr('id').data('placeholder-id')); | ||
$input = $input.hide().next().show().attr('id', $input.removeAttr('id').data('placeholder-id')); | ||
// If `clearPlaceholder` was called from `$.valHooks.input.set` | ||
if (event === true) { | ||
return $input[0].value = value; | ||
} | ||
$input.focus(); | ||
} else { | ||
$input.val('').removeClass('placeholder'); | ||
input.value = ''; | ||
$input.removeClass('placeholder'); | ||
} | ||
} | ||
} | ||
|
||
function setPlaceholder() { | ||
var $replacement, | ||
$input = $(this), | ||
input = this, | ||
$input = $(input), | ||
$origInput = $input, | ||
id = this.id; | ||
if ($input.val() === '') { | ||
if ($input.is(':password')) { | ||
if (input.value == '') { | ||
if (input.type == 'password') { | ||
if (!$input.data('placeholder-textinput')) { | ||
try { | ||
$replacement = $input.clone().attr({ 'type': 'text' }); | ||
|
@@ -84,18 +122,23 @@ | |
} | ||
$replacement | ||
.removeAttr('name') | ||
// We could just use the `.data(obj)` syntax here, but that wouldn’t work in pre-1.4.3 jQueries | ||
.data('placeholder-password', true) | ||
.data('placeholder-id', id) | ||
.data({ | ||
'placeholder-password': true, | ||
'placeholder-id': id | ||
}) | ||
.bind('focus.placeholder', clearPlaceholder); | ||
$input | ||
.data('placeholder-textinput', $replacement) | ||
.data('placeholder-id', id) | ||
.data({ | ||
'placeholder-textinput': $replacement, | ||
'placeholder-id': id | ||
}) | ||
.before($replacement); | ||
} | ||
$input = $input.removeAttr('id').hide().prev().attr('id', id).show(); | ||
// Note: `$input[0] != input` now! | ||
} | ||
$input.addClass('placeholder').val($input.attr('placeholder')); | ||
$input.addClass('placeholder'); | ||
$input[0].value = $input.attr('placeholder'); | ||
} else { | ||
$input.removeClass('placeholder'); | ||
} | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Actually, you can and it will set the
element.value
property by default which means you could remove theelse
block above as well. :)