Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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].

like image 628
akeeseth Avatar asked Apr 17 '26 09:04

akeeseth


1 Answers

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>
like image 192
Artem Vyshniakov Avatar answered Apr 19 '26 22:04

Artem Vyshniakov