Skip to content

Commit 6902601

Browse files
author
Michael
committed
Fix #686
Store the multiselectID to the widget instance and use that in refresh methods to prevent creating collisions in input IDs.
1 parent 1eb136f commit 6902601

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

src/jquery.multiselect.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*
2020
*/
2121
(function($, undefined) {
22-
22+
// Counter used to prevent collisions
2323
var multiselectID = 0;
2424
var $doc = $(document);
2525

@@ -77,6 +77,8 @@
7777
// factory cannot unbind automatically. Use eventNamespace if on
7878
// jQuery UI 1.9+, and otherwise fallback to a custom string.
7979
this._namespaceID = this.eventNamespace || ('multiselect' + multiselectID);
80+
// bump unique ID after assigning it to the widget instance
81+
this.multiselectID = multiselectID++;
8082

8183
var button = (this.button = $('<button type="button"><span class="ui-icon ui-icon-triangle-1-s"></span></button>'))
8284
.addClass('ui-multiselect ui-widget ui-state-default ui-corner-all')
@@ -133,9 +135,6 @@
133135
if(!o.multiple) {
134136
this.menu.addClass('ui-multiselect-single');
135137
}
136-
137-
// bump unique ID
138-
multiselectID++;
139138
el.hide();
140139
},
141140

@@ -159,8 +158,8 @@
159158
_makeOption: function(option) {
160159
var title = option.title ? option.title : null;
161160
var value = option.value;
162-
var id = this.element.attr('id') || multiselectID; // unique ID for the label & option tags
163-
var inputID = 'ui-multiselect-' + multiselectID + '-' + (option.id || id + '-option-' + this.inputIdCounter++);
161+
var id = this.element.attr('id') || this.multiselectID; // unique ID for the label & option tags
162+
var inputID = 'ui-multiselect-' + this.multiselectID + '-' + (option.id || id + '-option-' + this.inputIdCounter++);
164163
var isDisabled = option.disabled;
165164
var isSelected = option.selected;
166165
var labelClasses = [ 'ui-corner-all' ];

tests/unit/events.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,22 @@
127127
el.multiselect("destroy").remove();
128128
});
129129

130+
test("multiselectclick with multiple widgets", function() {
131+
expect(3);
132+
var first = $("<select multiple><option value='1'>Option 1</option><option value='2'>Option 2</option></select>").appendTo(body).multiselect();
133+
var second = $("<select multiple><option value='1'>Option 1</option><option value='2'>Option 2</option></select>").appendTo(body).multiselect();
134+
equals($('.ui-multiselect').length, 2, "two mutliselects are on the page");
135+
first.multiselect("refresh");
136+
second.multiselect("refresh");
137+
$label = $(second.multiselect("getLabels")[0]);
138+
$wrongInput = $(first.multiselect("getLabels")[0]).find("input");
139+
$label.click();
140+
equals($label.find("input").prop("checked"), true, "the input for that label should be checked");
141+
equals($wrongInput.prop("checked"), false, "the input for the corresponding label on the first widget should not be checked");
142+
first.multiselect("destroy").remove();
143+
second.multiselect("destroy").remove();
144+
});
145+
130146
test("multiselectclick", function(){
131147
expect(28);
132148

0 commit comments

Comments
 (0)