How to apply jquery effect to an element created by user Knockout.js

Hello, I have the following code in my opinion:

<div data-bind="foreach: Elements"> <div data-bind="attr:{id: id}"> <img data-bind="attr:{src: ImageSource}" /> <p data-bind="text: Name"></p> </div> </div> 

But for each new element, I want to add jQuery effet, for example:

  $("#draggable").draggable(); 

Is there a way to sign an event that occurs after an item is added to this list?

+7
source share
1 answer

The best way to do this is to use custom bindings.

 ko.bindingHandlers.draggable= { init: function(element, valueAccessor) { $(element).draggable(); } }; <div data-bind="foreach: Elements"> <div data-bind="attr:{id: id}, draggable: {}"> <img data-bind="attr:{src: ImageSource}" /> <p data-bind="text: Name"></p> </div> </div> 

See the documentation for more on this: http://knockoutjs.com/documentation/custom-bindings.html .

+10
source

All Articles