0

I have a customer who is a member of a web site. He has to fill a form every time which is really very often. That's why he wants me to develop an application for him to make this process automatic. When I use the webBrowser control to manipulate it, I am able to login but after that there are fields that contains data-binding. These fields are the ones I need to manipulate. When I push the data to necessary fields, it's not working, because in the html tag, there is no value attribute, instead it has data-binding. So my question is how can I manipulate and push data to these fields?

Thank you so much for your all help in advance.

Henry
  • 2,953
  • 2
  • 21
  • 34
Cagdas Arslan
  • 19
  • 1
  • 9
  • Did you read the documentation ? http://knockoutjs.com/documentation/attr-binding.html – PMerlet Dec 01 '16 at 14:47
  • yes actually, I have checked it and got the logic for data-binding. its like angularJs ng-model but the thing that i couldnt understand is manipulating it. if you have any idea on that, i would be grateful if you could share with me :) – Cagdas Arslan Dec 01 '16 at 14:53
  • is there any way to add a custom script into html dom?? via webBrowser control. If it is possible, maybe then could push the script into html to be able to manipulate??? – Cagdas Arslan Dec 01 '16 at 14:56

1 Answers1

0

Knockout uses data-binds to listen to changes in an input and update an underlying model. For example, the value binding listens to change events and writes the new value to a data-bound observable.

If you update a value attribute through code, the change event isn't triggered. You'll see the new value in the UI, but the javascript model won't be updated.

You can combat this by explicitly triggering a change. Here's an example:

  • Type in the input: you'll see a console.log that shows knockout gets updated
  • Press the button to inject a new value: you won't see a log: knockout isn't updated
  • Press the last button to trigger a change event. You'll notice knockout now updates the model.

Of course, you can combine the two click listeners into one function. I've separated them to get the point across.

// Hidden knockout code:
(function() {
  var label = ko.observable("test");
  label.subscribe(console.log.bind(console));

  ko.applyBindings({ label: label });
}());

// Your code
var buttons = document.querySelectorAll("button");
var input = document.querySelector("input");

buttons[0].addEventListener("click", function() {
  input.value = "generated value";
});

buttons[1].addEventListener("click", function() {
  // http://stackoverflow.com/a/2856602/3297291
  var evt = document.createEvent("HTMLEvents");
  evt.initEvent("change", false, true);
  input.dispatchEvent(evt);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<input type="text" data-bind="value: label">

<button>inject value from outside</button>
<button>let knockout know something changed</button>
user3297291
  • 22,592
  • 4
  • 29
  • 45
  • thanks for the share man, somehow i couldnt get the proper time to check it but as soon as possible i will try this then let you know. thanks again. – Cagdas Arslan Dec 04 '16 at 09:11