Reputation: 1816
I have one string which is having html code. So I am binding in view like
<div data-bind="html : Notes()"></div>
Now I want add one class to div based on length of Notes with out Html elements.
Suppose Notes = "<b>Hello</b>"
, then Notes.length = 12 (With <b> and </b>)
But I need to replace all html elements and I need to get length as 5(Hello).
How can I achieve this below line.
<div data-bind="html : Notes(), css:{myclass : Notes().length > 5}"></div>
Upvotes: 0
Views: 229
Reputation: 1335
You can add dynamic class by css property and then add static class by attr. property. Details are available in the below link. Do let me know if this does not help.
combine dynamic and static classes through css binding, knockout.js
Upvotes: 0
Reputation: 23372
You could make a computed that creates a virtual element and returns its text length like so:
var noteLength = ko.computed(function() {
var tempElement = document.createElement("div");
tempElement.innerHTML = Notes();
return tempElement.innerText.length;
});
Upvotes: 2