kumar
kumar

Reputation: 37

knockout js click binding not working

I used the example of knockout js and its not working. I don't know why click event is not firing.

HTML:

<div>
    You've clicked <span data-bind="text: numberOfClicks"></span> times
    <button data-bind="click: incrementClickCounter">Click me</button>
</div>

Javascript:

<script type="text/javascript">
    var viewModel = {
    numberOfClicks : ko.observable(0),
    incrementClickCounter : function() {
    alert("hi im click");
    var previousCount = this.numberOfClicks();
    this.numberOfClicks(previousCount + 1);
     }
    };
</script>

Upvotes: 1

Views: 13225

Answers (1)

Prashant Tapase
Prashant Tapase

Reputation: 2147

Try this code Go Through link

HTML:

<div>
    You've clicked <span data-bind="text: numberOfClicks"></span> times
    <button data-bind="click: incrementClickCounter">Click me</button>
</div>

Javascript:

<script src="~/Scripts/jquery-2.1.1.js"></script>
<script src="~/Scripts/knockout-3.2.0.js"></script>
<script type="text/javascript">
    var viewModel = {
    numberOfClicks : ko.observable(0),
    incrementClickCounter : function() {
    alert("hi im click");
    var previousCount = this.numberOfClicks();
    this.numberOfClicks(previousCount + 1);
     }
    };

ko.applyBindings(new viewModel());
</script>

Upvotes: 3

Related Questions