I've been trying to create a custom bindingHandler that i can use to give a watermark behaviour to text input fields.
By watermark
i mean: to add default values to text fields that are removed on focus, and replaced on blur if the text field is still empty
I have managed to get this to work as demonstrated in this jsfiddle: http://jsfiddle.net/rpallas/nvxuw/
I have 3 questions about this solution:
$(element).val('')
but this also felt wrong. Which is best, or is there a better way?I think you're use of allbindings is unecessary. In fact I don't think the watermark needs to be aware of the observable at all since that's what a watermark generally does i.e the placeholder
attribute.
Would this work for you?
ko.bindingHandlers.watermark = {
init: function (element, valueAccessor, allBindingsAccessor) {
var value = valueAccessor(), allBindings = allBindingsAccessor();
var defaultWatermark = ko.utils.unwrapObservable(value);
var $element = $(element);
setTimeout(function() {
$element.val(defaultWatermark);}, 0);
$element.focus(
function () {
if ($element.val() === defaultWatermark) {
$element.val("");
}
}).blur(function () {
if ($element.val() === '') {
$element.val(defaultWatermark)
}
});
}
};
http://jsfiddle.net/madcapnmckay/Q5yME/1/
Hope this helps.
The previous approach is fine as long as your app logic is really simple, be aware that solution is messing with the values of your View Model, those values can be observables and they can have subscriptions or computeds associate to it, so by changing the value you change your View Model. Here is a different solution without updating your View Model
ko.bindingHandlers.fakePlaceHolderWhenNeedIt = {
init: function (element, valueAccessor, allBindings, vm) {
if (!Modernizr.input.placeholder) {
var placeHolderVal = $(element).attr("placeholder");
if (placeHolderVal != null || placeHolderVal != '') {
var $element = $(element);
var value = valueAccessor()
var valueUnwrapped = ko.utils.unwrapObservable(value);
$element.keyup(function () {
var inputValue = $(this).val();
var $watermark = $(this).prev('.ie-placeholder');
if (inputValue == null || inputValue == '') {
$watermark.show();
}
else {
$watermark.hide();
}
});
var display = valueUnwrapped != null || valueUnwrapped != '' ? "block" : "none";
var left = $element.position().left;
var top = $element.position().top;
var paddingLeft = $element.css('padding-left');
var paddingRight = $element.css('padding-right');
var paddingTop = $element.css('padding-top');
var paddingBottom = $element.css('padding-bottom');
var height = $element.css('height');
var placeHolder = '<div class="ie-placeholder" style="position:absolute;left:' + left + ';top:' + top + ';padding-top: ' + paddingTop + ';padding-bottom: ' + paddingBottom + ';padding-left: ' + paddingLeft + ';padding-right: ' + paddingRight + ';height: ' + height + ';line-height:' + height + ';display:' + display + ';">' + placeHolderVal + '</div>';
$(placeHolder).click(function () { $element.focus(); }).insertBefore(element);
}
}
},
update: function (element, valueAccessor, allBindings, vm) {
if (!Modernizr.input.placeholder) {
var placeHolderVal = $(element).attr("placeholder");
if (placeHolderVal != null || placeHolderVal != '') {
var $element = $(element);
var value = valueAccessor()
var valueUnwrapped = ko.utils.unwrapObservable(value);
var $watermark = $element.prev('.ie-placeholder');
if (valueUnwrapped == null || valueUnwrapped == '') {
$watermark.show();
}
else {
$watermark.hide();
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With