Knockout.js ko.dataFor

mbird

I have a data-bind with input textbox

<input id="tColorCode" type="text" data-bind="value: ColorCode" />

I don't have access to the external JavaScript where the view model is setup so I don't have information I need about the object but if I click an element then in the handler I can change it's value:

$("#tColorCode").live("click", function () {
    //....
    ko.dataFor(this).ColorCode("#ffffff");
}

But I need to do that outside of the click handler and so I try with the element's name but it does not work:

var c = $("#tColorCode");
console.log(c); //OK
ko.dataFor(c).ColorCode("#ffffff"); //TypeError: ko.dataFor(...) is undefined

Can this be done another way?

Thank you!

dotnetom

You need to pass specific HTML element to method dataFor, however, variable c is jQuery selector, not an element.To get specific element from a selector you can simply get the first item of a selector:

var c = $("#tColorCode");
ko.dataFor(c[0]).ColorCode("#ffffff");  // use c[0] instead of c

The difference from the event handler case, is that in event hander this represents a specific element, not a selector, so it was working correctly.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Angular equivalent of Knockout ko.dataFor

From Dev

Angular equivalent of Knockout ko.dataFor

From Dev

Knockout Js - ko.utils.arrayFirst does not work

From Dev

What ko.applyBindings() is doing in knockout.js?

From Dev

Durandal Widget: Knockout "<!-- ko if: -->" anomaly

From Dev

Knockout JS Error: Cannot write a value to a ko.computed unless you specify a 'write' option

From Dev

Knockout JS - Invalid State Error using ko.toJSON and google maps

From Dev

How to filter a nested ko.observableArray from another array in knockout.js with checkboxes

From Dev

Knockout - ko.computed with ko.isObservable and peek()

From Dev

Knockout - Difference between ko.computed and ko.observable.subscribe

From Dev

Knockout mappingOptions for ko.mapping.toJSON()-method

From Dev

Knockout ko.mappings.fromJS not working

From Dev

knockout: ko observable without a default value

From Dev

Knockout ko.mappings.fromJS not working

From Dev

what is the difference between <!--ko--> binding and visible binding in knockout?

From Dev

Prevent Web Essentials JsHint "ko is not defined" warning when using Knockout

From Dev

Knockout validation doesn't show errors for ko.validatedObservable

From Dev

binding a bootstrap list-group to a knockout ko.observable

From Dev

what is the difference between <!--ko--> binding and visible binding in knockout?

From Dev

Is ko.mapping.fromJS method depreceated in knockout 2.2 and 3?

From Dev

ko.bindingHandlers.datepicker not working for knockout version 3.0

From Dev

Knockout Utility Functions - ko.utils.arrayFilter - data binding

From Dev

Knockout JS not binding

From Dev

Is there an equivalant of 'as' for with in knockout.js?

From Dev

Removing bindings in Knockout js

From Dev

knockout js and ajax

From Dev

Knockout js over mvc

From Dev

unchecking checkbox in knockout js

From Dev

knockout(js) to the (html)head

Related Related

  1. 1

    Angular equivalent of Knockout ko.dataFor

  2. 2

    Angular equivalent of Knockout ko.dataFor

  3. 3

    Knockout Js - ko.utils.arrayFirst does not work

  4. 4

    What ko.applyBindings() is doing in knockout.js?

  5. 5

    Durandal Widget: Knockout "<!-- ko if: -->" anomaly

  6. 6

    Knockout JS Error: Cannot write a value to a ko.computed unless you specify a 'write' option

  7. 7

    Knockout JS - Invalid State Error using ko.toJSON and google maps

  8. 8

    How to filter a nested ko.observableArray from another array in knockout.js with checkboxes

  9. 9

    Knockout - ko.computed with ko.isObservable and peek()

  10. 10

    Knockout - Difference between ko.computed and ko.observable.subscribe

  11. 11

    Knockout mappingOptions for ko.mapping.toJSON()-method

  12. 12

    Knockout ko.mappings.fromJS not working

  13. 13

    knockout: ko observable without a default value

  14. 14

    Knockout ko.mappings.fromJS not working

  15. 15

    what is the difference between <!--ko--> binding and visible binding in knockout?

  16. 16

    Prevent Web Essentials JsHint "ko is not defined" warning when using Knockout

  17. 17

    Knockout validation doesn't show errors for ko.validatedObservable

  18. 18

    binding a bootstrap list-group to a knockout ko.observable

  19. 19

    what is the difference between <!--ko--> binding and visible binding in knockout?

  20. 20

    Is ko.mapping.fromJS method depreceated in knockout 2.2 and 3?

  21. 21

    ko.bindingHandlers.datepicker not working for knockout version 3.0

  22. 22

    Knockout Utility Functions - ko.utils.arrayFilter - data binding

  23. 23

    Knockout JS not binding

  24. 24

    Is there an equivalant of 'as' for with in knockout.js?

  25. 25

    Removing bindings in Knockout js

  26. 26

    knockout js and ajax

  27. 27

    Knockout js over mvc

  28. 28

    unchecking checkbox in knockout js

  29. 29

    knockout(js) to the (html)head

HotTag

Archive