Created
May 26, 2016 19:26
-
-
Save zslabs/88fd26cdac40a9ba0ebd92b77229491e to your computer and use it in GitHub Desktop.
Harvest Chosen fallback mobile usability
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Know how https://harvesthq.github.io/chosen/ falls back to a normal <select> in unsupported browsers? The following is a workaround for still allowing use of the `data-placeholder` property when initiated, but a sane fallback when not. | |
HTML output: | |
<div class="ChosenSelect"> | |
<select required data-placeholder="Choose a fruit"> | |
<option value="">Choose a fruit</option> | |
<option value="Apples">Apples</option> | |
<option value="Mangos">Mangos</option> | |
<option value="Pears">Pears</option> | |
<option value="Plums">Plums</option> | |
<option value="Nectarine">Nectarine</option> | |
</select> | |
</div> | |
JS init: | |
$('.ChosenSelect select') | |
// Must run before Chosen is initiated | |
.on('chosen:ready', function(e, opts) { | |
// Check if first value is blank option | |
if (!e.target[0].value) { | |
// Update option to appease `data-placeholder` requirement | |
e.target[0].innerHTML = ''; | |
// Trigger update method | |
$(this).trigger('chosen:updated'); | |
} | |
}) | |
.chosen(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment