JavaScript jQuery: How to change data-bind on click event? jquery change data attribute,jquery change data attribute value,jquery

I have a a form with multiple fields on it. Below is example:

<input id="txtContactFirstName" name="txtContactFirstName" maxlength="20" class="k-textbox standardInput" required="required" data-bind="value: appointmentDetails.ContactFirstName" />
<input id="txtContactPhone" name="txtContactPhone"  data-role="maskedtextbox" data-mask="(999)-000-0000" class="k-textbox phoneInput" required="required" data-bind="value: appointmentDetails.ContactPhone" />

I have a checkbox on the form as well.

<label id="lblPatientDetailsSameAsPatient"><input id="patientDetailsSameAsPatientCheckBox" type="checkbox" data-bind="checked: intakeSettings.sameAsPatient" name ="PatientDetailsSameAsPatientCheckBox" />Same as Patient</label>

I want the data-bind values to change to something else when I click on the checkbox. So basically I will be pulling data for txtContactFirstName from patientDetails.FirstName and txtContactPhone from patient.Details.FirstName? How can I achieve this on click and assign data-bind values to txtContactFirstName and txtContactPhone ?

$("#patientDetailsSameAsPatientCheckBox").click(function (e) {
        if (e.target) {

           ?? Logic goes here which will assign data-bind values to txtContactFirstName  and txtContactPhone ?

        }
    });
Answer:1

You can use the jQuery method data to access data attributes on HTML elements, and set values. Also, I don't think you need the if (e.target) { logic you had there.

$("#patientDetailsSameAsPatientCheckBox").click(function (e) {

        $("#txtContactFirstName").data("bind",patientDetails.FirstName);
        $("#txtContactPhone").data("bind",patientDetails.Phone);

});

Here's the doc pertaining to the data method: http://api.jquery.com/data/

Answer:2

I would do it by adding a change event handler onto the observable your form is bound to. If the field that changed was intakeSettings.sameAsPatient, then setting properties on the observable that txtContactFirstName and txtContactPhone are bound to.

// What your form is bound to
var viewModel = new kendo.observable({
  patient: {
    FirstName: 'Ted',
    Phone: '123-456-7890'
  },
  appointmentDetails: {
    ContactFirstName: '',
    ContactPhone: ''
  },
  intakeSettings: {
    sameAsPatient: false
  }
});

viewModel.bind('change', function(e) {
  // e.field is the property that changed, e.sender is the observable
  if(e.field === 'intakeSettings.sameAsPatient') {
    if(e.sender.get(e.field) === true) {
      e.sender.set('appointmentDetails.ContactFirstName', e.sender.patient.FirstName);
      e.sender.set('appointmentDetails.ContactPhone', e.sender.patient.Phone);
    }
    else {
      e.sender.set('appointmentDetails.ContactFirstName', '');
      e.sender.set('appointmentDetails.ContactPhone', '');
    }
  }
});
Answer:3

I'm performing following scenario : When I press a button I insert this into the database and that's working. But i don't get feedback if it succeeded or not (the alerts wont pop up). But if I look in ...

I'm performing following scenario : When I press a button I insert this into the database and that's working. But i don't get feedback if it succeeded or not (the alerts wont pop up). But if I look in ...

I am using Grid List(dynamic tiles) in angularJS and using <br/> tag but it is not working Here is the code <div ng-controller="gridListDemoCtrl as vm" flex="" ng-cloak="" class="...

I am using Grid List(dynamic tiles) in angularJS and using <br/> tag but it is not working Here is the code <div ng-controller="gridListDemoCtrl as vm" flex="" ng-cloak="" class="...

Okay, I spent all morning trying to solve this, and despite the fact that it seems to have been a problem for a couple years, I couldn't find a solution that worked. I've seen other people on Stack ...

Okay, I spent all morning trying to solve this, and despite the fact that it seems to have been a problem for a couple years, I couldn't find a solution that worked. I've seen other people on Stack ...

  1. allow chrome to remember password

New with JavaScript. Can someone help me understand why calling print() returns undefined? class Quizer { constructor(quizObj) { this.quiz = quizObj; } print() { console....

New with JavaScript. Can someone help me understand why calling print() returns undefined? class Quizer { constructor(quizObj) { this.quiz = quizObj; } print() { console....

  1. javascript class constructor example
  2. javascript class constructor arguments
  3. javascript class constructor array
  4. javascript class constructor async
  5. javascript class constructor super
  6. javascript class constructor with parameters
  7. javascript class constructor function
  8. javascript class constructor overloading
  9. javascript class constructor call method
  10. javascript class constructor not called
  11. javascript class constructor shorthand
  12. javascript class constructor object
  13. javascript class constructor name
  14. javascript class constructor syntax
  15. javascript class constructor es6
  16. javascript class constructor not working
  17. javascript class constructor initialize
  18. javascript class constructor prototype
  19. javascript class constructor spread
  20. javascript class constructor destructuring