Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Button: support <input> tag wrapped in <label> tag. Fix #6063, Button doesn't support radio input inside label. #531

Closed
wants to merge 2 commits into from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions tests/unit/button/button.html
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
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