Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible to update Chosen's selected values? #1270

Closed
kiddrew opened this issue Jun 10, 2013 · 7 comments · Fixed by #1286
Closed

Possible to update Chosen's selected values? #1270

kiddrew opened this issue Jun 10, 2013 · 7 comments · Fixed by #1286

Comments

@kiddrew
Copy link

kiddrew commented Jun 10, 2013

I'm doing something like this in jquery:

var obj = $("select[name='myselect']");
obj.find('option').removeAttr('selected');
obj.trigger("liszt:updated");

I would expect that to update the list and remove all selected options. It does appear to be updating the list logically, but it's not updating the UI to reflect the deselected options. It simply shows the previously selected options with the data-placeholder text next to it (which I assume means the field is logically empty).

@KyleJSummers
Copy link

I believe that I'm having the exact same issue as well. I am removing all of the options from the native select element and I'm looking to regenerate the Chosen select. This functionality does work in older versions of Chosen (I tested it using the version from the documentation).

It looks like the current master has a change in Chosen.prototype.results_build in regards to the way Chosen counts selected choices:

if (this.is_multiple && this.choices_count() > 0) {
    this.search_choices.find("li.search-choice").remove();
}

It looks like it thinks there are no choices selected, so there is nothing to remove (which might be the case within the HTML select, but not Chosen's UI). Removing the choice_count() check in the if statement seems to resolve it, but I'm not sure if that would have adverse effects otherwise. Perhaps it's unnecessary since the jQuery remove call that it executes would still be fine even if there was nothing to remove.

The following commit might be to blame: a469c43

@kiddrew
Copy link
Author

kiddrew commented Jun 11, 2013

Thanks for the workaround Kyle. I'm not seeing any adverse effects so far.

@pfiller
Copy link
Contributor

pfiller commented Jun 17, 2013

You should not update an object's attributes this way (this helpful stack overflow thread gets into why). If you're just looking to blow away all selections, jQuery's val function makes this easy:

$("field").val("").trigger("liszt:updated")

@pfiller pfiller closed this as completed Jun 17, 2013
@KyleJSummers
Copy link

@pfiller , there is definitely a bug here. Please see this fiddle. If you click "Reset" to remove the selections, Chosen doesn't update properly.

It looks like it's only a problem with multiple type select inputs. Whether I remove the "selected" attribute or unset the value of the select, the problem remains the same.

@pfiller
Copy link
Contributor

pfiller commented Jun 17, 2013

@KyleJSummers Weird. Something seems to have been introduced between 0.9.14 and 0.9.15 that broke this functionality. Good times!

@pfiller pfiller reopened this Jun 17, 2013
@KyleJSummers
Copy link

@pfiller as I referenced in my earlier comment, it seems like it has to do with commit a469c43

@pfiller
Copy link
Contributor

pfiller commented Jun 19, 2013

Thanks @KyleJSummers - this is now resolved in master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants