Skip to content

Commit d48884a

Browse files
committed
Fixed issue tuupola#11 by implementing suggested code
Coverted variables describing what's currently selected into an array. This breaks the tests, as it appears not to be compatible with the support for multiple parents to a selector.
1 parent ec5582c commit d48884a

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

Diff for: jquery.chained.js

+19-14
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,13 @@
3535
$(self).html(backup.html());
3636

3737
/* If multiple parents build classname like foo\bar. */
38-
var selected = "";
38+
var selected = [];
3939
$(parent_selector).each(function() {
40-
if ($(":selected", this).val()) {
41-
selected += "\\" + $(":selected", this).val();
42-
}
40+
$(":selected", this).each(function() {
41+
selected.push($(this).val());
42+
});
4343
});
44-
selected = selected.substr(1);
4544

46-
/* Zepto class regexp dies with classes like foo\bar. */
47-
if (window.Zepto) {
48-
selected = selected.replace("\\", "\\\\");
49-
}
5045
/* Also check for first parent without subclassing. */
5146
/* TODO: This should be dynamic and check for each parent */
5247
/* without subclassing. */
@@ -59,11 +54,21 @@
5954
var selected_first = $(":selected", first).val();
6055

6156
$("option", self).each(function() {
62-
/* Remove unneeded items but save the default value. */
63-
if (!$(this).hasClass(selected) &&
64-
!$(this).hasClass(selected_first) && $(this).val() !== "") {
65-
$(this).remove();
66-
}
57+
58+
59+
var hasSelectedClass = false;
60+
var classList = ($(this).attr('class') == undefined || "" ) ? [] : $(this).attr('class').split(/\s+/);
61+
$.each( classList, function(index, item){
62+
if (jQuery.inArray(item, selected) >= 0) {
63+
hasSelectedClass = true;
64+
return;
65+
}
66+
});
67+
if (!hasSelectedClass &&
68+
!$(this).hasClass(selected_first) && $(this).val() !== "") {
69+
$(this).remove();
70+
}
71+
6772
});
6873

6974
/* If we have only the default value disable select. */

0 commit comments

Comments
 (0)