5

I am trying to check/uncheck a jquery-ui checkboxradio widget programatically (i.e. without using the mouse). I've tried all manner of things but not joy. To reproduce the issue, choose a blank html page with just jquery and jquery-ui script tags and then create the widget dynamically:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Blank</title>
    <link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>
<body>
</body>
</html>

After the page loads, I hit F12 to bring up dev-tools and enter the following commands to get a minimal jquery-ui checkbox:

$container = $("<div>")
$container.append($("<label for='chkbox'>"))
$container.append($("<input type='checkbox' id='chkbox'>"))
$container.appendTo('body')
$('#chkbox').checkboxradio()

The checkbox is unchecked by default. Lets try to check it:

$('#chkbox').attr('checked', true) // nope
$('#chkbox').checkboxradio('refresh') // nope
$('#chkbox').attr('checked', false)
$('#chkbox').prop('checked', true) // nope
$('#chkbox').prop('checked', false)
$('#chkbox').addClass('ui-checkboxradio-checked') // not this either
$('#chkbox').removeClass('ui-checkboxradio-checked')
$('#chkbox').checkboxradio('option', 'classes.ui-checkboxradio-checked', true) // does nothing
$('#chkbox').checkboxradio('option', 'ui-checkboxradio-checked', true) // does nothing
$('#chkbox').checkboxradio('option', 'classes.ui-checkboxradio.ui-checkboxradio-checked', true) // and again, nothing...

This is irritating. Does anyone have a solution to this seemingly simple problem?

2 Answers 2

6

I hope it should be help you thanks.

$container = $("<div>")
$container.append($("<label for='chkbox'>"))
$container.append($("<input type='checkbox' id='chkbox' >"))
$container.appendTo("body")
 $("#chkbox").checkboxradio();
$("#chkbox").prop("checked",true).checkboxradio("refresh")
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Blank</title>
    <link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>
<body>
</body>
</html>

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

1 Comment

Any reason why you do not have consistency in your apostrophe? The HTML portion has double apostrophe for attributes, yet the generated HTML from JQuery has single apostrophe.
3

You need to call change function as jquery ui will change the visual effect on change so only settiing the checked property wont take any effect

$("#chkbox").attr("checked","checked").change();

1 Comment

There is refresh() method for that widget. Also, jQuery specs says that you shoud use prop() method for setting properties like checked, selected, or disabled

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.