javascript - Knockoutjs does not update viewmodel -
in view have file input:
<input type="file" class="file-input" name="file_source" size="40" onchange=''>
and span, in showing uploaded filename:
<span class='label label-info' id="upload-file-info" data-bind="text: image"></span> $(".file-input").change(function() { var elem = $("#upload-file-info"); elem.html = $(this).val(); });
this span binded knockoutjs:
viewmodel = { image: ko.observable() } ko.applybindings(viewmodel);
the problem observable not updates when update span text. althought have filename in span, observable empty. how can make observable update when span text changes ?
i did quick fiddle according comment on question. should work:
jquery(document).ready(function ($) { 'use strict'; $(".file-input").change(function () { var elem = $("#upload-file-info"); viewmodel.image($(this).val()); }); var viewmodel = { image: ko.observable() }; viewmodel.image.subscribe(function (value) { alert(value); }); ko.applybindings(viewmodel); });