0

I have KO custom binding on an input element and need it to detect if there's been a change (or addition) of a css class for that element. Is this something I detect using a custom binding, or not?

hotdiggity
  • 329
  • 7
  • 23

1 Answers1

2

It is usually not a good idea to mix Knockout and other methods to manipulate the DOM.

So in your case it is probably a better idea to set the CSS class using knockout maybe with a computed. See knockout js css multiple class bindings

If the class manipulation is outside your control (for example because it is done by an external plugin). Then you can use something like the Attr change jquery plugin to detect the DOM change and then set an observable with the result (which can then be detected by your custom binding)

Community
  • 1
  • 1
leszek.hanusz
  • 5,152
  • 2
  • 38
  • 56
  • A commendable solution. To be more specific, I'm using KnockoutJS-Validation plug-in which adds an error class to the input field. I need to detect an invalid field and have this passed to the custom binding so I can run jQuery plug-in on that element. Any ideas appreciated pls - http://stackoverflow.com/questions/21405518/knockoutjs-validation-binding-value-passed-to-custom-binding – hotdiggity Jan 28 '14 at 12:37
  • 1
    you can use isValid() See http://stackoverflow.com/questions/14776948/knockout-validation-plugin-get-isvalid-from-specific-property – leszek.hanusz Jan 28 '14 at 12:41