Bug in FAQs for checkboxes
|
View:
New views
4 Messages
—
Rating Filter:
Alert me
|
|
|
Bug in FAQs for checkboxesHi, 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. |
|
|
Re: Bug in FAQs for checkboxesHi, 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...@...> 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. |
|
|
Re: Bug in FAQs for checkboxesHi Greg,
Since this sounds like it could be a bug in the jQuery source code, not just in the FAQs, would you mind posting what you've discovered to the jquery-dev list (http://groups.google.com/group/jquery-dev)? It'll probably get more attention there, and someone may suggest that you file a bug report if they're able to confirm the problem. Thanks so much, --Karl ____________ Karl Swedberg On Jan 25, 2009, at 2:21 AM, Greg Glockner wrote:
|
|
|
Re: Bug in FAQs for checkboxesAck! Sorry, Greg. After replying to you I checked the jquery-dev moderation queue and, lo and behold, you had already posted there. I've promoted you to member of the jquery-dev list, so your emails shouldn't get stuck in moderation from here on out.
cheers, --Karl On Jan 25, 2009, at 2:38 PM, Karl Swedberg wrote:
|
| Free embeddable forum powered by Nabble | Forum Help |