0

I am using http://harvesthq.github.io/chosen/ in multiple mode and it dosen't seem to update after submitting. All I get is a blank input box and in the dropdown, the entry I just submitted is still there. I know the entry is getting submitted because of the json return. If I refresh the page, the entry is no longer in the dropdown and the default I use is there. Where have I gone wrong with my coding.

Many thanks

$("#box_rtv").val('Choose a box...').trigger("chosen:updated");
<select data-placeholder="Choose a box..." class="chosen-select required" multiple="multiple" style="width:250px;" name="box_rtv[]" id="box_rtv">
user1532468
  • 1,723
  • 8
  • 41
  • 80

3 Answers3

1

First of all, you're triggering the event on a string with your code, as .val() returns a string. Try doing it separately, or in a different order while chaining:

$("#box_rtv").trigger("chosen:updated").val('Choose a box...');

(Actually, you probably don't need to call .val() at all.)

If that doesn't work, try deselecting all of your options programatically prior to triggering chosen:updated:

$("#box_rtv > option").prop('selected', false);
$("#box_rtv").trigger("chosen:updated");
rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
1

The way you're calling the .trigger() is wrong.

$("#box_rtv").val('Choose a box...').trigger("chosen:updated"); //WRONG

Instead simply

After appending all the values to the select, just trigger as mentioned below

$("#box_rtv").trigger("chosen:updated");
Praveen
  • 55,303
  • 33
  • 133
  • 164
  • No difference. still not updating. Thanks – user1532468 May 07 '14 at 11:48
  • @user1532468 I'm not sure how you're appending the options to the chosen, here look into this [**answer**](http://stackoverflow.com/a/20148252/1671639) – Praveen May 07 '14 at 12:16
  • Praveen could you expand on what you mean by appending the options to the chosen? Thanks – user1532468 May 07 '14 at 16:25
  • @user1532468 I managed to create a sample fiddle http://jsfiddle.net/praveen_jegan/LUHLd/. I hope you can understand it. – Praveen May 08 '14 at 04:47
  • Praveen That is ok for adding an option to the select dropdown, but that is not what I am trying to do. I am adding the items dynamically and when a user selects an item is submit the item to the database and refresh or remove the item from the list. Doese that make sense? Thanks – user1532468 May 08 '14 at 09:29
  • Any further help with this please Praveen. It;s driving me nuts. Thanks – user1532468 May 09 '14 at 09:31
  • @user1533468 hmm .. it would be better if you can show the exact part of the code that you've written, so I can look into that perspective and help you.. – Praveen May 10 '14 at 04:17
  • It seems to me that chosen only updates the dropdown to a default state. It doesn't actually remove selected items from the list. Should i be looking at some other way to repopulate the dropdown? Thanks – user1532468 May 10 '14 at 10:18
1

In case you remove and option these actions going right in this order:

$('#user_id').val(''); // to clean the select ( otherwise not refreshing correctly )
$("#user_id option[value='okmAdmin']").remove(); // remove some option element
$('#user_id').trigger("chosen:updated"); // 
darkman97i
  • 170
  • 7