1

When selecting values on chosen lists on a page, then leaving the page, then coming back, the values are still set in the chosen selects. No matter what I do.

I've already searched a lot for this. I've tried the following:

$(".chzn-select").val('').trigger("liszt:updated");
$(".chzn-select").val('').trigger("chosen:updated");

I've tried to add optionsCaption in data-bind, like here.

I am desperately resetting my value observables to null, '', [], whatever, nothing works.

Any super trick here?

Thanks

Update: here's code, if that can help. Html:

<select class="chzn-select left" multiple id="selectsector" data-bind="optionsCaption: '', options: sectors(), selectedOptions: selectedSectors, optionsText: 'name'"></select>

And JS:

var sectors = ko.observableArray(),
    selectedSectors = ko.observableArray();

I populate sectors with standard js objects, nothing fancy and it works well.

Nicolas

Community
  • 1
  • 1
Nicolas
  • 356
  • 1
  • 5
  • 17

2 Answers2

1

All you need to do is set the observable's value to null inside of your view model -

selectedSectors([]);

You cannot use jQuery to clear a Knockout observable's value.

PW Kad
  • 14,953
  • 7
  • 49
  • 82
-1

I had a case where my observable was one way by design. Setting the observable to null did not work. To reset it, I used

selectedSelectors(undefined);

wclark
  • 436
  • 4
  • 14
  • Thanks for the downvote. How is this unhelpful? I was looking for a solution, and found this post. Others will find it, and see my solution. Maybe it will help them. – wclark Nov 08 '13 at 13:36