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:
- Is there any way to change it so that I only have to declare the watermark value once? Currently I have to put it on the place where I declare the binding and I also have to initialise the observable with the same value in the viewModel - as it will otherwise have no initial value.
- Is there a better way of getting to the underlying observable that the elements value is bound to. I'm currently grabbing it using the allBindingsAccessor, but this feels wrong to me. Originally I was just setting the value using jquery
$(element).val('')
but this also felt wrong. Which is best, or is there a better way? - Does any one have or know of an existing solution to this this problem? Am I re-inventing the wheel?