I have a select element where i bind the css class using Jquery Knockout.
I want to access the current element instance directly from data-bind attribute, without creating a property in the ViewModel (because i have many select elements which shares the same functionality)
Is this possible?
<select id="select1" data-bind="css: { 'no-value-selected': $item.val() == '' }">
<option value="">[Select a value]</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
EDIT Using ViewModel to achieve this (what i want to avoid)
function ViewModel() {
this.select1HasNoValueSelected = ko.computed(function () {
return $("#select1").val() == '';
}, this);
};
var viewModel = new ViewModel();
ko.applyBindings(viewModel);
<select id="select1" data-bind="css: { 'no-value-selected': select1HasNoValueSelected }">