knockout.js - Get ViewModel from DOM element

Solution 1:

Knockout has two utility methods that might help here.

  • ko.dataFor will return the ViewModel that the element is bound to.
  • ko.contextFor returns the "binding context" of the current element. The object you get back from this method will return something like:

    { 
        $data: ...,
        $parents,
        $root
    }
    

So if I understand your question, you can probably use ko.dataFor here. Here's a simple example using dataFor.