I had a similar problem. I come up with a solution reminiscent of Backbone.js using el and $ el links.
in your ViewModel:
var myViewModel = function(){ var self = this;
in html (e.g. list item):
<li class="myclass" data-bind="el: el, $el: $el"></li>
in bindHandlers (showing all possible arguments for init):
ko.bindingHandlers.el = { init: function(element, valueAccessor, allBindings, viewModel, bindingContext) { var value = valueAccessor();
For example, you can use $ el, for example:
var myViewModel = function(){ var self = this; //plain DOM element reference self.el = ko.observable(); //jquery object reference self.$el = ko.observable(); self.myFunction = function() { console.log(self.$el().html()); self.$el().addClass("myCssClass"); } }
Hope this helps!
Murat Ozgul Dec 29 '15 at 9:45 2015-12-29 09:45
source share