how to create a knockout hint text custom binding?

79 views
Skip to first unread message

Farhad Taran

unread,
Apr 29, 2013, 9:48:59 AM4/29/13
to knock...@googlegroups.com

Im trying to create a custom binding to show hint texts in text inputs.

so far I have this but it doesnt work:

ko.bindingHandlers.hintText= {
    init: function (element, valueAccessor) {
        element.focus(function () {
            if ($(this).val() === defaultText) {
                $(this).attr("value", "");
            }
        });
        element.blur(function () {
            if ($(this).val() === '') {
                $(this).val(valueAccessor());
            }
        });
    }
}

the html:

<input id="profileID" type="text" data-bind="hintText:'Enter Profile ID'" />

Patrick Steele

unread,
Apr 29, 2013, 10:33:56 AM4/29/13
to knock...@googlegroups.com
Not sure if these are problems, but:

1. What is "defaultText"?  It's not defined anywhere in your sample.
2. I think "element.focus" and "element.blur" should be "$(element).focus" and "$(element).blur".
3. Are you sure the "this" context is the element and not the bindingHandler?

--
You received this message because you are subscribed to the Google Groups "KnockoutJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to knockoutjs+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Casey Corcoran

unread,
Apr 29, 2013, 11:01:02 AM4/29/13
to knock...@googlegroups.com
Here's a quick one I built recently


Takes the initial "value" attribute as the defaultText and wraps the "value" binding.

-- 
Casey Corcoran
Sent with Sparrow

Reply all
Reply to author
Forward
0 new messages