building jquery checkbox - can't set checked value

Posted by benpage on Stack Overflow See other posts from Stack Overflow or by benpage
Published on 2010-03-19T01:04:51Z Indexed on 2010/03/19 1:11 UTC
Read the original article Hit count: 331

Filed under:
|

this seems straightforward enough, I don't know if it's a bug, or just something I'm doing wrong.

What I want to do is build a checkbox input in jquery, then return the string of the input. I have this code:

var obj = $('<input>');
obj.attr('disabled', 'disabled');
obj.attr('type', 'checkbox');
obj.attr('checked', 'checked'); // this does not work!

no matter how i try to render this obj, the 'checked' attribute never gets outputted.

So I have tried:

$('<div>').append(obj).remove().html();

and it returns:

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

I have tried setting an id attribute, it makes no difference. I have also tried this but it doesn't work:

obj.attr('checked', true);

Any suggestions on how to get the input tag rendered with the checked attribute set to 'checked' (via jquery)?

I realise I can just do this with string concatenation, but now I really want to know why it's not working the way it should.

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about checkbox