Get bound property name in a KnockoutJS custom binding

In a KnockoutJS custom binding, you may want to retrieve the name of the bound property. This usually isn’t necessary; you can reference the valueAccessor parameter to get the bound data without knowing the property’s name. But a recent project raised the need.

The custom binding that I wrote mirrors the syntax of the foreach binding, which accepts a few different formats. These are all valid:

Let’s look at a sample custom binding.

The bound data is passed as the valueAccessor parameter, but I couldn’t find a reliable way to retrieve the name. So instead I parsed the properties of the bound DOM node, the element parameter.

In the dataBind.match, be sure to replace “myBindingThis approach would return “property” in the examples above.

One thought on “Get bound property name in a KnockoutJS custom binding

Leave a Reply

Your email address will not be published.