On my view I have an option to add users, each user
is a div with a few fields:
<div>
<input data-bind="value: name" />
<input data-bind="value: position" />
<input data-bind="value: email" /></div>
<button data-bind="click: function(){ $root.addUser()}">SAVE</button>
</div>
This div is being added by a button click, and didn't exist when the page loaded.
When I try to save the new user by clicking on the SAVE button, it doesn't work, nothing happens, it just ignores my click.
So my question is- how can I make knockout data-bind
work on dynamically generated element?
I read a few posts online but couldn't find a solution, any help would be appreciated.
Users.js
script:
var User = function (data) {
this.name= ko.observable(data.name);
this.position= ko.observable(data.position);
this.email= ko.observable(data.email);
}
self.users = ko.observableArray([]);
self.addUser= function () {
console.log("test");
}
And this is the code that adds the div: Users.ui.js
:
$('body').on('click', '.add', function () {
var strClientSecHtml = "<div class=\"sec\"><div class=\"secIn\">\n...
$(strClientSecHtml).insertBefore($(this).closest('.sec'));
showPrimaryLbl();
})