I have a button with a html binding which I render at the beginning. The reason I want to render this button like this is because I want to delete this button (delete, not remove visibility) later.
By script is as follows:
<script type="text/javascript">
function AppViewModel() {
var self = this;
self.addButton = ko.observable();
var button = "<button class='btn btn-primary btn-lg' type='button' data-bind=\"click: buttonClicked, customVal: val\"> Add button</button>";
self.addButton(button);
self.buttonClicked = function() {
alert('Button clicked');
};
}
$( document ).ready(function() {
ko.applyBindings(new AppViewModel());
});
</script>
My HTML is:
<div class="col-md-6">
<div id="addButt" data-bind="html: addButton"></div>
</div>
Now when I load the page the button is correctly rendered but when I click it nothing happens. It doesn't call the 'buttonClicked' method.
My understanding is that this is because the DOM is not updated to this new button when bindings are applied.
Is there anyway to re-apply the bindings with the new DOM?
Cheers.