Highlight text on page
HTML
<script src="http://johannburkard.de/resources/Johann/jquery.highlight-5.closure.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.3.0/knockout-debug.js"></script>
<label>Search : <label />
<input data-bind="textInput:searchText"></input><br />
<a href="http://johannburkard.de/blog/programming/javascript/highlight-javascript-text-higlighting-jquery-plugin.html" target="_blank"> Highlight Web Page </a>
<div class="searchme">
Text higlighting is part of DynaCloud – where tags/keywords are automatically highlighted once they’re clicked – so I took that code and made it a stand-alone JavaScript text highlighting jQuery plugin.
</div>
<br/>
<div data-bind="highlighter: searchText">
Text higlighting is part of DynaCloud – where tags/keywords are automatically highlighted once they’re clicked – so I took that code and made it a stand-alone JavaScript text highlighting jQuery plugin.
</div>
CSS
.highlight { background-color: yellow }
JavaScript
ko.bindingHandlers.highlighter = {
init: function (element, valueAccessor, allBindingsAccessor) {
var observable = valueAccessor();
$(element).highlight(observable());
},
update: function (element, valueAccessor) {
var value = ko.utils.unwrapObservable(valueAccessor());
$(element).removeHighlight();
if(value != null && value != "") {
$(element).highlight(value);
}
}
};
$('.searchme').highlight('high');
var vm= { searchText : ko.observable("query") };
ko.applyBindings(vm);