Skip to content

Commit

Permalink
Fix #686
Browse files Browse the repository at this point in the history
Store the multiselectID to the widget instance and use that in refresh
methods to prevent creating collisions in input IDs.
  • Loading branch information
Michael committed Mar 16, 2017
1 parent 1eb136f commit 6902601
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
11 changes: 5 additions & 6 deletions src/jquery.multiselect.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*
*/
(function($, undefined) {

// Counter used to prevent collisions
var multiselectID = 0;
var $doc = $(document);

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

var button = (this.button = $('<button type="button"><span class="ui-icon ui-icon-triangle-1-s"></span></button>'))
.addClass('ui-multiselect ui-widget ui-state-default ui-corner-all')
Expand Down Expand Up @@ -133,9 +135,6 @@
if(!o.multiple) {
this.menu.addClass('ui-multiselect-single');
}

// bump unique ID
multiselectID++;
el.hide();
},

Expand All @@ -159,8 +158,8 @@
_makeOption: function(option) {
var title = option.title ? option.title : null;
var value = option.value;
var id = this.element.attr('id') || multiselectID; // unique ID for the label & option tags
var inputID = 'ui-multiselect-' + multiselectID + '-' + (option.id || id + '-option-' + this.inputIdCounter++);
var id = this.element.attr('id') || this.multiselectID; // unique ID for the label & option tags
var inputID = 'ui-multiselect-' + this.multiselectID + '-' + (option.id || id + '-option-' + this.inputIdCounter++);
var isDisabled = option.disabled;
var isSelected = option.selected;
var labelClasses = [ 'ui-corner-all' ];
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,22 @@
el.multiselect("destroy").remove();
});

test("multiselectclick with multiple widgets", function() {
expect(3);
var first = $("<select multiple><option value='1'>Option 1</option><option value='2'>Option 2</option></select>").appendTo(body).multiselect();
var second = $("<select multiple><option value='1'>Option 1</option><option value='2'>Option 2</option></select>").appendTo(body).multiselect();
equals($('.ui-multiselect').length, 2, "two mutliselects are on the page");
first.multiselect("refresh");
second.multiselect("refresh");
$label = $(second.multiselect("getLabels")[0]);
$wrongInput = $(first.multiselect("getLabels")[0]).find("input");
$label.click();
equals($label.find("input").prop("checked"), true, "the input for that label should be checked");
equals($wrongInput.prop("checked"), false, "the input for the corresponding label on the first widget should not be checked");
first.multiselect("destroy").remove();
second.multiselect("destroy").remove();
});

test("multiselectclick", function(){
expect(28);

Expand Down

0 comments on commit 6902601

Please sign in to comment.