Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions tests/unit/button/button.html
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ <h2 id="qunit-userAgent"></h2>
<input type="radio" id="radio23" name="radio" checked="checked"><label for="radio23">Choice 3</label>
</div>
</form>
<form>
<div id="radio3" style="margin-top: 2em;">
<label><input type="radio" id="radio31" name="radio">Choice 1</label>
<label><input type="radio" id="radio32" name="radio">Choice 2</label>
<label><input type="radio" id="radio33" name="radio" checked="checked">Choice 3</label>
</div>
</form>

<input type="checkbox" id="check"><label for="check">Toggle</label>

Expand Down
33 changes: 33 additions & 0 deletions ui/jquery.ui.button.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,26 @@ $.widget( "ui.button", {
if ( this.type === "checkbox" || this.type === "radio" ) {
// we don't search against the document in case the element
// is disconnected from the DOM

//if the <input>'s parent is <label>( ie. implicit label), convert into explicit label.
if ( this.element.parent( "label" ).length ) {
//used when destroy -> convert back to implicit label.
this.isImplicit = true;
var id = this.element.attr( "id" );
if ( !id ) {
//get the idList of :ui-button and :not([id])( we haven't built yet)
var idList=$( ":ui-button" ).filter( "[id^='ui-button-'], :not([id])" ).map(function(){
return this.id.substr(10);
}).get();
var max=Math.max.apply(Math,idList);
id = "ui-button-" + (max+1);
//set new_id = current_max+1, in case of duplicated
this.element.attr( "id", id);
}
this.element.parent( "label" ).attr( "for", id);
this.element.insertBefore(this.element.parent( "label" ));
}

var ancestor = this.element.parents().last(),
labelSelector = "label[for='" + this.element.attr("id") + "']";
this.buttonElement = ancestor.find( labelSelector );
Expand Down Expand Up @@ -262,6 +282,19 @@ $.widget( "ui.button", {
.removeAttr( "role" )
.removeAttr( "aria-pressed" )
.html( this.buttonElement.find(".ui-button-text").html() );
if ( this.isImplicit ) {
if ( this.buttonElement.attr( "for" ).indexOf( "ui-button-" ) !== -1 ) {
this.buttonElement.removeAttr( "for" );
this.element.removeAttr( "id" );
}
this.element.prependTo(this.buttonElement);
}
if ( this.buttonElement.attr( "class" ) === "") {
this.buttonElement.removeAttr( "class" );
}
if ( this.element.attr( "class" ) === "") {
this.element.removeAttr( "class" );
}

if ( !this.hasTitle ) {
this.buttonElement.removeAttr( "title" );
Expand Down