Mode:
Duration:
1
Coding works best on desktop or with an external keyboard.
Coding works best on desktop or with an external keyboard.
Counter that cycles through multiple color themes.
<div class="container" data-bind="css: theme()">
<h2>Counter: <span data-bind="text: count"></span></h2>
<div>
<button data-bind="click: increment">+</button>
<button data-bind="click: decrement">-</button>
<button data-bind="click: reset">Reset</button>
</div>
<button data-bind="click: toggleTheme">Switch Theme</button>
</div>
<script>
function CounterViewModel() {
this.count = ko.observable(0);
this.themes = ['light-theme','dark-theme','blue-theme'];
this.current = 0;
this.theme = ko.observable(this.themes[this.current]);
this.increment = () => { this.count(this.count() + 1); };
this.decrement = () => { this.count(this.count() - 1); };
this.reset = () => { this.count(0); };
this.toggleTheme = () => { this.current = (this.current + 1) % this.themes.length; this.theme(this.themes[this.current]); };
}
ko.applyBindings(new CounterViewModel());
</script>Knockout.js is a lightweight JavaScript library that uses the MVVM (Model-View-ViewModel) pattern to create responsive, data-driven UIs with declarative bindings. It emphasizes simplicity, observables, and automatic UI updates.
Origin & Creator
Created by Steve Sanderson at Microsoft in 2010 to simplify dynamic web UIs using the MVVM pattern.
Industrial Note
Knockout.js is still used in long-lived enterprise systems, internal dashboards, and large legacy codebases due to its stability, backward compatibility, and lightweight footprint.