What are the differences between Chosen and Select2?
Chosen and Select2 are the two more popular libraries for extending selectboxes.
Both seem to be actively maintained, Chosen is older and supports both jQuery and Prototype.
Select2 is jQuery only, its documentation says Select2 is inspired by Chosen, but doesn't detail any improvements made (if any) or other reasons for the rewrite.
Two libraries have pretty much same feature set, the only comparison I've found is a somewhat inconclusive jsperf test page.
Does any of these libraries have any advantages over the other?
Solution 1:
As of Select2 3.3.1, below are what's documented in its README.md
What Does Select2 Support That Chosen Does Not?
- Working with large datasets: Chosen requires the entire dataset to be loaded as
option
tags in the DOM, which limits it to working with small-ish datasets. Select2 uses a function to find results on-the-fly, which allows it to partially load results.- Paging of results: Since Select2 works with large datasets and only loads a small amount of matching results at a time it has to support paging. Select2 will call the search function when the user scrolls to the bottom of currently loaded result set allowing for the 'infinite scrolling' of results.
- Custom markup for results: Chosen only supports rendering text results because that is the only markup supported by
option
tags. Select2 provides an extension point which can be used to produce any kind of markup to represent results.- Ability to add results on the fly: Select2 provides the ability to add results from the search term entered by the user, which allows it to be used for tagging.
Solution 2:
IMHO Chosen is "maintained" but not "actively maintained". 341 issues and 51 pull requests for Chosen. Select2 has 128 issues and 25 pull requests. I think the pattern for these is basically
- pick whichever one is superficially more appealing to you
- use it in an app or two
- bump up against customization problems or limitations
- maybe try to work with the community via issues & pull requests
- eventually get fed up and just build your own using what you learned in this process
Whichever one you pick, if your use case is exactly in their sweet spot, either one will work. If not, you'll eventually have to write your own or heavily customize these. In either case, the choice of which one specifically isn't all that important. I guess I'll side with @Andy Ray and @paul here that Select2 is probably the better initial choice.
Solution 3:
Another difference worth mentioning is that Chosen
is developed in Sass
and CoffeeScript
whereas Select2
is plain CSS
and JS
. It is my personal option that Sass
and CoffeeScript
are unneeded layers of complexity which make debugging difficult.
After trying both I have decided to use neither - trying to get Select2
create item functionality turns out to be a very hairy affair as you simply can not do it when attached to <select>
elements - it just didn't feel well thought out the hoops I would have to jump through.
I have settled on using selectize.js which just adds the new <option>...</option>
element to the form's DOM - and that is sane. It does also use LESS
- but I would bypass that and just tailor the compiled CSS
directly in your project.