user4671373
user4671373

Reputation:

how to get data about checkbox is checked in javascript file

how to get data when checkbox is checked.

     MyClient =ko.observable(false);

this is the select option.

I used

   self.MyClient.subscribe(function(newValue){ code goes here...})

but it is not what I am looking for, as I click it, it does what I want but again when I unselect same happens because, in both cases value of MyClient is new.

Upvotes: 0

Views: 127

Answers (1)

Roy J
Roy J

Reputation: 43881

[Updated] You can make a field required by binding the required attribute (attr binding) to a boolean. Use the same boolean as is bound to your checkbox. CSS outlines the field in red when it is required in my example.

self = {
  myClient: ko.observable(false),
  myValue: ko.observable(''),
  message: function() {
    console.debug("Valu:", self.myValue());
    if (self.myClient() && !(self.myValue())) {
      return "Please enter value";
    }
  }
};

ko.applyBindings(self);
input[required] {
  outline: solid red 2px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<input type="checkbox" data-bind="checked: myClient" />
<input data-bind="attr: {required: myClient}, value: myValue" /><span data-bind="text:message()"></span>

Upvotes: 1

Related Questions