Skip to content

Instantly share code, notes, and snippets.

@SimonRice
Created September 16, 2011 14:56
Show Gist options
  • Select an option

  • Save SimonRice/1222289 to your computer and use it in GitHub Desktop.

Select an option

Save SimonRice/1222289 to your computer and use it in GitHub Desktop.
A modified version of jDoubleSelect 0.3, altered to work jQuery 1.6.4 on optgroups with spaces
/*
* jDoubleSelect jQuery plugin
*
* Copyright (c) 2010 Giovanni Casassa (senamion.com - senamion.it)
*
* Dual licensed under the MIT (MIT-LICENSE.txt)
* and GPL (GPL-LICENSE.txt) licenses.
*
* http://www.senamion.com
*
*/
(function ($) {
jQuery.fn.jDoubleSelect = function (o) {
o = $.extend({
text: ""
}, o);
return this.each(function () {
var el = $(this);
var name1 = (el.attr('id') || el.attr('name') || el.attr('class') || 'internalName') + '_jDS';
var name2 = name1 + "_2";
groupSel = "";
$(this).children("optgroup, option").each(function (i) {
// Verify disabled or selected group
if ($(this).attr("disabled"))
s = " disabled ";
else if ($(this).find(":selected").attr("value")) {
s = " selected ";
}
else s = "";
if ($(this).is("option"))
groupSel += "<option " + s + " class='jds_isOption' value='" + $(this).val() + "'>" + $(this).text() + "</option>";
else
groupSel += "<option " + s + " value='" + $(this).attr("label") + "'>" + $(this).attr("label") + "</option>";
});
$(this).hide().after("<select name='" + name1 + "' id='" + name1 + "'>" + groupSel + "</select> <span>" + o.text + "</span>");
$("#" + name1).change(function () {
// REMOVE OLD ELEMENT, ADD NEW SELECT, BIND CHANGE EVENT AND TRIGGER IT
if ($("#" + name2).length > 0)
$("#" + name2).remove();
if (!$(this).find(":selected").hasClass("jds_isOption")) {
el.next().next().after("<select id='" + name2 + "' >" + el.find("optgroup[label='" + $(this).val() + "']").html() + "</select>");
$("#" + name2).trigger("change");
} else {
el.attr("value", $(this).val());
}
});
$("#" + name2).live("change", function () {
el.attr("value", $(this).val());
}).trigger("change");
$("#" + name1).trigger("change")
});
};
})(jQuery);
@SimonRice

Copy link
Copy Markdown
Author

Changes made on b9b012 courtesy of "Alex"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment