Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add the checked="checked" attribute to the output of .html() method

There must be something special about the checked attribute of a checkbox either at the jQuery level or the DOM level. With this HTML:

<html>
    <body>
        <input id="cb" type="checkbox" />
    </body>
</html>

And this JavaScript:

$(function() {

    $("#cb").attr("checked","checked");
    alert("Expecting this HTML fragment to have a 'checked' attribute:\n\n"
          + $("body").html());

});

I'm not getting what I expect; if I use an attribute name besides "checked" it works as expected.

The reason I'm trying to get this to work is I need to get the HTML as it exists in a browser DOM and convert it to PDF, so users can get PDF versions of the page as it is as they're looking at it; it's manipulated heavily on the client side, so getting the correct HTML directly from the server is not an easy option.

Here's a working version of the above code: http://jsfiddle.net/pettys/qTfDP/2/

like image 385
pettys Avatar asked Jan 23 '26 03:01

pettys


1 Answers

Update to jQuery 1.6. There have been some significant changes to attr in 1.6/1.6.1.

like image 83
robertc Avatar answered Jan 24 '26 21:01

robertc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!