0

When I put this code in my console, I end up with a checked box in my web page:

$('body').append($('<input/>', {
    type: 'checkbox',
    value: "foo",
    checked: false
}));

When I remove the checked attr altogether, the box is not checked:

$('body').append($('<input/>', {
    type: 'checkbox',
    value: "foo",                                                                  
}));

...but I need a dynamic set of checkboxes to be generated, some checked and some not. What attr/value creates the correct "unchecked" state?

UPDATE:

I am using the latest Chrome. jQuery version 3.2.1 The code works as expected in this fiddle: https://jsfiddle.net/mmvunhL0/ But not on my website... I'm wondering if there is a global setting or something touching the checkbox behavior.

18
  • 3
    What browser are you using? You're first example works for me in chrome/IE Commented Dec 13, 2017 at 17:58
  • I'm using latest Chrome Commented Dec 13, 2017 at 17:59
  • using SO's version of jQuery (1.12) both examples append an unchecked checkbox. Commented Dec 13, 2017 at 17:59
  • XHTML allows checked to have a value. In HTML the checked attribute doesn't allow a value. I Commented Dec 13, 2017 at 17:59
  • Works fine for me: jsfiddle.net/g73c17wh Commented Dec 13, 2017 at 18:00

3 Answers 3

3

You can use .prop or .attr for the checkbox

<div id="body">

</div>

<script src="https://code.jquery.com/jquery-1.12.4.min.js" integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ=" crossorigin="anonymous"></script>
<script>   
var value = true;

$('#body').append($('<input/>', {
                                    type: 'checkbox',
                                    value: "foo",
                                }).prop('checked', value) ) ;
                                
value = false;                                
$('#body').append($('<input/>', {
                                    type: 'checkbox',
                                    value: "foo",
                                }).prop('checked', value) );   
                                
</script>

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

1 Comment

Not exactly necessary to chain the method.
3

When used with the checkbox, the attribute checked is a boolean attribute.

If it exists at all then it is true.

<input type="checkbox" checked />

is the same as

<input type="checkbox" checked="false" />

and

<input type="checkbox" checked="checked" />

The only way to not have it checked is by not having the checked attribute at all.

2 Comments

In JS checked: false is correctly used as OP has. There is difference between checked: false and checked: "false".
@panther is correct. I am following the rules of the API and checked:false should result in an unchecked box.
0

You're getting unchecked on both of your code. Because, by default the checkbox is unchecked and if you want it to be checked explicitly, then define checked to true.

$('body').append($('<input/>', {
    type: 'checkbox',
    value: "foo",
    checked: true
}));

If you want it randomly, then you can use Math:

$('body').append($('<input/>', {
    type: 'checkbox',
    value: "foo",
    checked: Boolean(Math.round(Math.random()))
}));

Boolean is necessary here. It's because the property value strictly look for true of false value but not truthy or falsy value.

1 Comment

@mhodges That's html and in html if you just define checked attribute then that is identified by true status.

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.