I'm working on making a few tweaks to a project that uses Knockout, and I can't for the life of me figure out how to update the value of a multiselect from jQuery. I'm sure there's ways to do it natively in Knockout, but it's a tiny change and I really don't want to have to dig into it.
The html for the select box is this:
<select id="projectSelect"
data-bind="multiSelect: { items: projects, value: filteredProject, header: false, multiple: false },
optionsText: 'name',
optionsValue: 'id',
optionsCaption: 'All Projects'"
style="display: none;">
</select>
And defined in Knockout like this:
this.filteredProject = ko.observable(null);
this.filteredProject.subscribe(function(projectId)
{
for (var i = 0; i < self.projects.length; i++)
if (self.projects[i].id == projectId)
{
self.filteredProjectObject(self.projects[i]);
self.selectedProjectCheckboxDisabled(false);
return
}
self.onlyShowHoursForSelectedProject(false);
self.selectedProjectCheckboxDisabled(true);
self.filteredProjectObject(null)
});
this.filteredProjectObject = ko.observable(null);
And my current jQuery is :
var projSelect = $("#projectSelect");
projSelect.val(projId).change();
projSelect.multiselect("refresh").change()
And that jQuery runs on a button press.
When the jQuery runs, the multiselect updates with the new value, but the code tied to it never runs, and no matter what I do I can't force Knockout to update. Tearing my hair out here, so any help will be very much appreciated.