Skip to content

Commit 0b30a1d

Browse files
ddstreetscottgonzalez
authored andcommitted
Button: find associated label even without common ancestor. Fixes #7092 - button creation that requires a matching label does not find label in all cases
1 parent 0eb1106 commit 0b30a1d

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

tests/unit/button/button_tickets.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,26 @@ test( "#6262 - buttonset not applying ui-corner to invisible elements", function
2020
ok( set.find( "label:eq(2)" ).is( ".ui-button.ui-corner-right" ) );
2121
});
2222

23+
test( "#7092 - button creation that requires a matching label does not find label in all cases", function() {
24+
var group = $( "<span><label for='t7092a'/><input type='checkbox' id='t7092a'/></span>" );
25+
group.find( "input:checkbox" ).button();
26+
ok( group.find( "label" ).is( ".ui-button" ) );
27+
28+
group = $( "<input type='checkbox' id='t7092b'/><label for='t7092b'/>" );
29+
group.filter( "input:checkbox" ).button();
30+
ok( group.filter( "label" ).is( ".ui-button" ) );
31+
32+
group = $( "<span><input type='checkbox' id='t7092c'/></span><label for='t7092c'/>" );
33+
group.find( "input:checkbox" ).button();
34+
ok( group.filter( "label" ).is( ".ui-button" ) );
35+
36+
group = $( "<span><input type='checkbox' id='t7092d'/></span><span><label for='t7092d'/></span>" );
37+
group.find( "input:checkbox" ).button();
38+
ok( group.find( "label" ).is( ".ui-button" ) );
39+
40+
group = $( "<input type='checkbox' id='t7092e'/><span><label for='t7092e'/></span>" );
41+
group.filter( "input:checkbox" ).button();
42+
ok( group.find( "label" ).is( ".ui-button" ) );
43+
});
44+
2345
})( jQuery );

ui/jquery.ui.button.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,16 @@ $.widget( "ui.button", {
201201
if ( this.type === "checkbox" || this.type === "radio" ) {
202202
// we don't search against the document in case the element
203203
// is disconnected from the DOM
204-
this.buttonElement = this.element.parents().last()
205-
.find( "label[for=" + this.element.attr("id") + "]" );
204+
var ancestor = this.element.parents().last(),
205+
labelSelector = "label[for=" + this.element.attr("id") + "]";
206+
this.buttonElement = ancestor.find( labelSelector );
207+
if ( !this.buttonElement.length ) {
208+
ancestor = ancestor.length ? ancestor.siblings() : this.element.siblings();
209+
this.buttonElement = ancestor.filter( labelSelector );
210+
if ( !this.buttonElement.length ) {
211+
this.buttonElement = ancestor.find( labelSelector );
212+
}
213+
}
206214
this.element.addClass( "ui-helper-hidden-accessible" );
207215

208216
var checked = this.element.is( ":checked" );

0 commit comments

Comments
 (0)