akeeseth
akeeseth

Reputation: 845

Dynamic title attribute in knockoutjs

I want to set title attribute of span dynamically. I have tried below:

<span id="aPublic" class="pointer" 
 data-bind="attr:{title: {'mark private': isPublic, 'mark public': !isPublic()}}">
</span>

But it gives me [object Object].

Upvotes: 3

Views: 4168

Answers (1)

Artem Vyshniakov
Artem Vyshniakov

Reputation: 16465

You cannot do this in such way. Create computed value in your view model that will return needed title depends on isPublic property:

self.title = ko.computed(function(){
   return self.isPublic() ? 'mark private' : 'mark public';
});

Or you can do this inside data-bind attribute but it is not considered the best of solutions:

<span id="aPublic" class="pointer" 
 data-bind="attr:{title: isPublic() ? 'mark private': 'mark public'}">
</span>

Upvotes: 4

Related Questions