Hi, it's worse than I originally thought. Put my sample code on a PHP
server, and load the page from a browser. Using removeAttr does,
indeed, remove the checked attribute and unchecks the box.  Next,
click on the box to select it, then press the button marked Uncheck -
good. This time, jQuery does NOT uncheck the box.  This is a bug.

I've been digging into the source code for jQuery 1.2.6 and 1.3.1, and
here's what I've observed.  First, jQuery 1.3.1 detects the page as
XML (XHTML), while jQuery 1.2.6 does not.  (If you simply put my code
in a file without sending the headers, then jQuery does not detect the
page as XML).  With 1.2.6, jQuery.attr sets notxml to true (saying
it's not XML) while 1.3.1 sets notxml to false (saying the page is
XML).  From here, I get a bit lost inside the jQuery source code, but
what I could determine is that the code marked "If applicable, access
the attribute via the DOM 0 way" is able to modify the checked
property, while the alternate code (when notxml is false) is unable to
modify the checked property.

I've got a workaround - to set the DOM checked property directly to
true/false, but I thought someone should research this issue.

Thanks, Greg Glockner.


On Jan 24, 8:24 pm, Greg Glockner <greg.glock...@gmail.com> wrote:
> Hi, after some painful debugging, I determined that there is a bug in
> the jQuery FAQs about how to clear a checkbox.  Specifically, if you
> have a strict XHTML document, you *cannot* use .attr("checked","") to
> clear a checkbox. Instead, you must use .removeAttr("checked").  See
> the following PHP code sample:
>
> <?
>         header('Content-type: application/xhtml+xml');
>         print '<?xml version="1.0" encoding="iso-8859-1"?>' . "\n";
> ?>
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
>       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd";>
> <html xmlns="http://www.w3.org/1999/xhtml";>
> <head>
> <title>Checkbox test</title>
> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/
> libs/jquery/1.3/jquery.min.js"></script>
> </head>
> <body>
> <p>
> <input type="checkbox" id="c"/> I'll be checked/unchecked.
> <input type="button" value="Check" onclick='$("#c").attr
> ("checked","checked")'/>
> <input type="button" value="Uncheck - fail" onclick='$("#c").attr
> ("checked","")'/>
> <input type="button" value="Uncheck - good" onclick='$("#c").removeAttr
> ("checked")'/>
> </p>
> </body>
> </html>
>
> I have updated the FAQ accordingly. Hope this helps.

Reply via email to