Skip to content

Commit 2477039

Browse files
committed
Data attributes from option items are now copied to input elements.
Fixes #554 Access data attributes with: var input = $(selector).multiselect("instance").inputs[x] var data = $(input).data()
1 parent ffd19b3 commit 2477039

File tree

2 files changed

+34
-32
lines changed

2 files changed

+34
-32
lines changed

src/jquery.multiselect.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@
183183
"aria-selected": isSelected ? "true" : null,
184184
"disabled": isDisabled ? "disabled" : null,
185185
"aria-disabled": isDisabled ? "true" : null
186-
}).appendTo($label);
186+
}).data($(option).data()).appendTo($label);
187187

188188
$("<span/>").text($(option).text()).appendTo($label);
189189

tests/unit/methods.js

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,44 @@
44

55
test("open", function(){
66
expect(2);
7-
7+
88
el = $("select").multiselect().multiselect("open");
99
ok( el.multiselect("isOpen"), "isOpen parameter true" );
1010
equals( menu().css("display"), "block", "Test display CSS property" );
1111
el.multiselect("destroy");
1212
});
13-
13+
1414
test("close", function(){
1515
expect(2);
16-
16+
1717
el = $("select").multiselect().multiselect("open").multiselect("close");
1818
ok( !el.multiselect("isOpen"), "isOpen parameter false" );
1919
equals( menu().css("display"), "none", "Test display CSS property" );
2020
el.multiselect("destroy");
2121
});
22-
22+
2323
test("enable", function(){
2424
expect(2);
25-
25+
2626
el = $("select").multiselect().multiselect("disable").multiselect("enable");
2727
ok( button().is(":disabled") === false, "Button is enabled" );
2828
ok( el.is(":disabled") === false, "Original select is enabled" );
2929
el.multiselect("destroy");
3030
});
31-
31+
3232
test("disable", function(){
3333
expect(2);
34-
34+
3535
// clone this one so the original is not affected
3636
el = $("select").clone(true).appendTo(body).multiselect().multiselect("disable");
3737
ok( button().is(":disabled"), 'Button is disabled');
3838
ok( el.is(":disabled"), 'Original select is disabled');
3939
el.multiselect("destroy").remove();
4040
});
41-
41+
4242
test("enabling w/ pre-disabled tags (#216)", function(){
4343
expect(5);
44-
44+
4545
el = $('<select><option disabled value="foo">foo</option><option value="bar">bar</option>')
4646
.appendTo(document.body)
4747
.multiselect();
@@ -60,27 +60,27 @@
6060
equals(disabled.data(key), undefined, "and the option no longer has the stored data flag");
6161
el.multiselect("destroy").remove();
6262
});
63-
63+
6464
test("widget", function(){
6565
expect(1);
66-
66+
6767
el = $("select").multiselect();
6868
ok( menu().is("div.ui-multiselect-menu"), 'Widget is the menu element');
6969
el.multiselect("destroy");
7070
});
71-
71+
7272
test("getButton", function(){
7373
expect(1);
74-
74+
7575
el = $("select").multiselect();
7676
var button = el.multiselect("getButton");
7777
ok( button.is("button.ui-multiselect"), 'Button is the button element');
7878
el.multiselect("destroy");
7979
});
80-
80+
8181
test("checkAll", function(){
8282
expect(1);
83-
83+
8484
el = $("select").multiselect().multiselect("checkAll");
8585
var inputs = menu().find("input");
8686
ok( inputs.filter(":checked").length === inputs.length, 'All inputs selected on the widget?');
@@ -89,15 +89,15 @@
8989

9090
test("uncheckAll", function(){
9191
expect(1);
92-
92+
9393
el = $("select").multiselect().multiselect("checkAll").multiselect("uncheckAll");
9494
ok( menu().find("input:checked").length === 0, 'All inputs unchecked on the widget?');
9595
el.multiselect("destroy");
9696
});
9797

9898
test("isOpen", function(){
9999
expect(2);
100-
100+
101101
el = $("select").multiselect().multiselect("open");
102102
ok( el.multiselect("isOpen"), 'Testing isOpen method after calling open method');
103103
el = $("select").multiselect("close");
@@ -107,25 +107,25 @@
107107

108108
test("destroy", function(){
109109
expect(2);
110-
110+
111111
el = $("select").multiselect().multiselect("destroy");
112112
ok( !$(".ui-multiselect").length , 'button.ui-multiselect removed from the DOM');
113113
ok( !el.data("multiselect") , 'no more multiselect obj attached to elem');
114114
});
115115

116116
test("getChecked", function(){
117117
expect(2);
118-
118+
119119
el = $("select").multiselect().multiselect("checkAll");
120120
equals( el.multiselect("getChecked").length, 9, 'number of checkboxes returned after checking all and calling getChecked');
121121
el.multiselect("uncheckAll");
122122
equals( el.multiselect("getChecked").length, 0, 'number of checkboxes returned after unchecking all and calling getChecked');
123123
el.multiselect("destroy");
124124
});
125-
125+
126126
test("getUnchecked", function(){
127127
expect(2);
128-
128+
129129
el = $("select").multiselect().multiselect("checkAll");
130130
equals( el.multiselect("getUnchecked").length, 0, 'number of checkboxes returned after checking all and calling getUnchecked');
131131
el.multiselect("uncheckAll");
@@ -134,27 +134,29 @@
134134
});
135135

136136
test("refresh", function(){
137-
expect(4);
138-
137+
expect(6);
138+
139139
el = $("select").clone().appendTo(body).multiselect();
140-
el.empty().html('<option value="foo">foo</option><option value="bar">bar</option>');
140+
el.empty().html('<option value="foo" data-testval=123>foo</option><option value="bar">bar</option>');
141141
el.multiselect('refresh');
142-
142+
143143
var checkboxes, getCheckboxes = (function hai(){
144144
checkboxes = menu().find('input[type="checkbox"]');
145145
return hai;
146146
})();
147-
147+
148148
equals( checkboxes.length, 2, "After clearing the select, adding 2 options, and refresh(), only 2 checkboxes exist");
149149
equals( checkboxes.eq(0).val(), 'foo', 'first is foo' );
150-
equals( checkboxes.eq(1).val(), 'bar', 'second is foo' );
151-
150+
equals( checkboxes.eq(1).val(), 'bar', 'second is bar' );
151+
152152
// add one more w/ append, just for safety's sake
153-
el.append('<option value="baz">baz</option>');
153+
el.append('<option value="baz" data-testval="something">baz</option>');
154154
el.multiselect('refresh');
155155
getCheckboxes();
156-
equals( checkboxes.eq(2).val(), 'baz', 'after an append() call, the third option is now' );
157-
156+
equals( checkboxes.eq(2).val(), 'baz', 'after an append() call, the third option is now baz' );
157+
equals($(el.multiselect("instance").inputs[0]).data().testval, 123, "the first input has the data attribute testval with value 123");
158+
equals($(el.multiselect("instance").inputs[2]).data().testval, "something", "the third input has the data attribute testval with value something");
159+
158160
el.multiselect("destroy").remove();
159161
});
160162

0 commit comments

Comments
 (0)