How to get a viewmodel item to update when html is updated.

66 views
Skip to first unread message

nevf

unread,
Jun 18, 2011, 3:53:35 AM6/18/11
to knock...@googlegroups.com
I've got an <input> element and I want to set it's value in code. ie elem.val( "Foo" ) which works fine. However the viewmodel item bound to this element is not updated. Is there some way I can get the view model to update. I don't know what viewmodel item that is bound to the HTML element.


---------------

Andrew Booth

unread,
Jun 18, 2011, 9:31:25 AM6/18/11
to knock...@googlegroups.com
Hi Neville


It looks like you may have to call .trigger('change') or .change() on the input, for value binder to be notified.

Andy


File name: <span data-bind='text: fileName'></span><br/>
<input id="fileName" data-bind='value: fileName'/>
<button id="setFileName">Set file name to 'knockout.jpg'</button>


var viewModel {
    fileNameko.observable()    
};

$('#setFileName').click(function({
    //$('#fileName').val('knockout.jpg'); // does not work
    $('#fileName').val('knockout.jpg').change()// works
});

ko.applyBindings(viewModel);

nevf

unread,
Jun 18, 2011, 6:53:50 PM6/18/11
to knock...@googlegroups.com
Hi Andrew, perfect, thanks a bunch. I can move forward again. ;-)
Reply all
Reply to author
Forward
0 new messages