3

I am trying to use the jQueryUI button widget, especially the option to turn checkbox into nice buttons.

The normal button works fine, but the checkbox doesn't!

Here is my code

HTML:

<button id="button">button</button>
<label for="input">
    <input id="input" type="checkbox"/>Checkbox
</label>

JavaScript:

$('#button').button();
$('#input').button();

I can't understand why it doesn't work on checkbox!!

DEMO

3
  • Where is the documentation that says you can turn a checkbox into a button? Commented Jun 9, 2012 at 22:43
  • 1
    @AshBurlaczenko it's in the second paragraph of the Overview section from the link provided by OP :) Commented Jun 9, 2012 at 22:48
  • @AshBurlaczenko jqueryui.com/demos/button/#checkbox Commented Jun 9, 2012 at 22:51

2 Answers 2

7
+50

Solution:
Change html to:

<button id="button">button</button>
<label for="input">
    Checkbox
</label>
<input id="input" type="checkbox"/>

And it will work properly, demo.

Explanation:
If you check the jQuery-UI code, especially the function _determineButtonType (line 7069 on version 1.8.18) you will find

var ancestor = this.element.parents().filter(":last"),
            labelSelector = "label[for='" + this.element.attr("id") + "']";
this.buttonElement = ancestor.find( labelSelector );

So jQuery-UI assumes that it can find the label within the last parent of the input, while in your example, the label is the last parent.
I think this is a bug that should be fixed, because many developers have the habbit to write the label just like that.

Sign up to request clarification or add additional context in comments.

Comments

3
<button id="button">button</button>
<label for="input">Checkbox</label>
<input id="input" type="checkbox"/>

http://jsfiddle.net/cY7fe/2/

The checkbox should'nt have been within the label, but rather next to it. It seems to be working now.

2 Comments

Thanks, seen skafandri's explanation?
Why yes I have. Quite thorough, he deserves the 'answer' points

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.