Igor
Igor

Reputation: 4003

About knockoutjs - how to data-bind with text: and click:

How are you guys? I'm new to the framework Knockoutjs, I wonder if I'm doing it right. (Need a little help.) :)

I have a tag and would contain the same data-bind = text: anything and click: any function. The question is: Is it possible to do this? This is the correct way? Follow what I'm talking about: (I am using an example from the website itself) where when the user clicks the "Click Me" he adds +1 on the counter. I want that when the user clicks the tag call the same function. :)

HTML:

<div>You've clicked <span data-bind='text: numberOfClicks, click: registerClick'>&nbsp;   </span> times</div>

JS:

var ClickCounterViewModel = function() {
this.numberOfClicks = ko.observable(0);

this.registerClick = function() {
    this.numberOfClicks(this.numberOfClicks() + 1);
};

this.resetClicks = function() {
    this.numberOfClicks(0);
};

this.hasClickedTooManyTimes = ko.computed(function() {
    return this.numberOfClicks() >= 3;
}, this);

};

ko.applyBindings(new ClickCounterViewModel());

Follows the file jsFiddle: Here

Many thanks in advance.

Upvotes: 1

Views: 3881

Answers (1)

Damien
Damien

Reputation: 8987

Yes it is possible to have several binding pairs in one data-bind attribute.

You just have separated them with a comma ,

You asked : I want that when the user clicks the tag call the same function. But you already code it.

You can click on the counter or on the button it increments the counter.

Upvotes: 2

Related Questions