0

I have an input

<input type="text" name="Name" id="Name" data-bind="value: Name" style="display: none;" >

and a View Model:

name  : ko.observable()

The name value is added to the input when I click a link in another control.

But the 'change' event is not firing.

How can I get the value of the input to change?

deltree
  • 3,756
  • 1
  • 30
  • 51
Htmllancer
  • 21
  • 5

1 Answers1

0

Perhaps, you can replace the change event by subscribing to changes of the observable (with the same effect).

<input type="text" name="Name" id="Name" data-bind="value: name" style="display: none;">

var Model = function() {
  var self = this;
  this.name = ko.observable();  
  this.name.subscribe(function () {
        alert(self.name());                
  });
};
ko.applyBindings(new Model());
gbs
  • 3,529
  • 2
  • 20
  • 18
  • Thanks. But I decide to use custom ko.bindingHandlers ko.bindingHandlers.updateAction = { update: function (el) { ... my actions here ... } } – Htmllancer Sep 19 '12 at 13:22
  • change event is not automatically triggered when changing value of input programmatically (see also [here](http://stackoverflow.com/questions/5760873/trigger-change-event-when-setting-selects-value-with-val-function)). After changing the value, you need to explicitly trigger the change event. In jquery: $('input').val("aa").change(). And only then, you will be able to handle the change in your custom binding. – gbs Sep 19 '12 at 13:56