Skip to content

Instantly share code, notes, and snippets.

@mcsheffrey
Created October 17, 2013 06:41
Show Gist options
  • Save mcsheffrey/7020088 to your computer and use it in GitHub Desktop.
Save mcsheffrey/7020088 to your computer and use it in GitHub Desktop.
Useful for implementing custom dropdowns with native open states. Tested on iOS6/7. iPhone/iPad and stock Android browser running 4.0.3
<select id="option-style-select" name="option_style">
<option value="Tee (American Apparel)" selected>Tee (American Apparel)</option>
<option value="Canvas Ringspun Tee">Canvas Ringspun Tee</option>
<option value="Bella Ladies Relaxed Fit Tee">Bella Ladies Relaxed Fit Tee</option>
</select>
<button>Open Select</button>
<script>
$("button").click(function() {
var element = $("select")[0], worked = false;
if (document.createEvent) {
var e = document.createEvent("MouseEvents");
e.initMouseEvent("mousedown", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
worked = element.dispatchEvent(e);
} else if (element.fireEvent) {
worked = element.fireEvent("onmousedown");
}
if (!worked) {
alert("It didn't worked in your browser.");
}
});
</scirpt>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment