4

I have some checkboxes in a button set and I want to use Jquery to check one of the checkboxes.

It works, however, the visual representation of the checkbox does not update. ie, after executing $('#chk').attr('checked',true); the checked value is true, but the checkbox apears unchecked.

Demoed here: http://jsfiddle.net/kralco626/jzVjT/1/

Thanks!

3
  • 1
    Note: when you click the button in the jsfiddle example, the checkbox is being checked. You can tell because when you click the checkbox after clicking the button the checkbox still appears in the "unchecked" state. Commented Dec 6, 2010 at 14:16
  • Nick Craver's answer should work. As an aside, I'd say if what you really want is a buttonset, then you probably want to have your original source be a group of radio buttons, not checkboxes. Usually a buttonset provides a group of mutually exclusive options, whereas checkboxes each toggle an individual property on or off (those turn into toggle buttons when you call .button() on them directly). Commented Dec 6, 2010 at 14:20
  • I want checkbox functionality, not radio button functionality. A Jquery buttonset will either be a checkbox type set or a radio button type set based on whether it contains inputs of type checkbox, or radio. Commented Dec 6, 2010 at 14:20

2 Answers 2

16

You need to call the refresh method on the buttonset to update the visual state after programmatic changes to the checked state, like this:

$("#test").buttonset("refresh");

You can test it out here.

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

Comments

-1

Try

'true'

rather than

true

Attributes are always string values.

Edit:

Never mind. No clue on buttonset functionality but things aren't behaving normally. Check the documentation for the API.

3 Comments

This is incorrect, it's a boolean value, and 'false' would set it to checked, unexpectedly.
Oh. Ick. You're right, but I don't like that JQ does it that way.
Yes, attr is confusing in that it accesses properties and not attributes... usually, except when it doesn't. jQuery tries to hide the difference, but the difference is still there.

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.