$(document).ready(function () {
$("select.magic").each(function () {
var select = $(this);
if (!select.hasOwnProperty("removed")) {
select.removedExtraneous = [];
}
var filterFunction = function () {
var oldVal = select.val();
select.children("option.extraneous").each(function () {
//save to return it later
select.removedExtraneous.push($(this));
$(this).detach();
});
//select the matching "proper" option
select.val(oldVal);
};
select.bind("keydown", function (event) {
if (event.keyCode == 32) {
filterFunction();
}
});
select.bind("mousedown", filterFunction);
select.bind("blur", function () {
//select the "proper" equivalent
var oldVal = select.val();
select.children("option")
.not(".extraneous")
.filter(function () {
return this.value == oldVal
}).prop("selected", "true");
//return the removed elements so it works on tab back
if (select.append) {
var option;
while (option = select.removedExtraneous.pop()) {
select.append(option);
}
}
});
});
});
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.