JQuery Quirks

Here are two interesting quirks in the way JQuery works. Let’s say you want to check all the check-boxes on the page - how do you do it? It’s trivial:

$(":checkbox").attr("checked", "true");

Now quickly, how do you un-check all the check boxes on the page? If you said:

$(":checkbox").attr("checked", "false");

You are wrong. Despite the fact that if you use a more standard notation and say for example:

document.myform.mycheckbox.checked = false;

it will give you the desired result. The JQuery call however does not work. In fact, it does the exact opposite - it will set all the check-boxes on the page to their checked state. Apparently for the few funky attributes like “checked” and “selected” the second argument of the attr() method call can be any non-empty string. It makes sense because you actually don’t have to give these attributes an explicit value in your HTML code for them to work. So how do you un-check a box in JQuery? There are two ways. First one, and the more sure-fire one is to remove the checked attribute altogether:

$(":checkbox").removeAttr("checked");

Second one which has worked for me for some reason was to set the attribute to the empty string:

$(":checkbox").attr("checked", "");

Same goes for select attributes in combo boxes and list boxes. You need to remove them or set them to an empty string, or they won’t go away.

Quirk number two: axjax weirdness. Look at the code below:

$("a").hover(function() {// do something});
$("#sometrigger").click(function() { 
   $("#container").load("/some/page.html #links"); });

What am I doing here? I’m adding some hover functionality to all the links on the page. Then I add an on-click even to #sometrigger element. When it is clicked I want to load the contents of #links from /some/page.html into #container asynchronously. Where is the quirk? The hover functionality will not apply to the dynamically loaded links. It’s weird, but that’s just how it works. So if you want this functionality on all your links you will need to reapply them:

$("a").hover(function() {// do something});
$("#sometrigger").click(function() { 
   $("#container").load("/some/page.html #links", 
      $("#container > a").hover(function() {// do something});
         });
   );

It’s a little bit silly, but it kinda makes sense. You may not always want to apply the same events and formating to newly loaded html. But if you don’t expect it, it may throw you off. One you know about it however you learn to compensate for it. I’m being told that folks coming from the Prototype community are hitting this little quirk like a brick wall because their framework behaves in the opposite way (ie. the effects and behaviors are applied to the ajax loaded html).

Related Posts:

  • 3 Value Checkbox with JQuery
  • Making a Better Use of Script Tags
  • Excel: Adding Checkboxes the Easy Way
  • JQuery Fun
  • Client Side Table Sorting with JQuery
  • Serializing Javascript Objects into Cookies
  • What Language Are you Coding in Right Now?
  • What Programming Language Should we Teach to CS Majors?
  • Making Websites Without Serverside Scripting
  • Dentists is teh Suck

  • 3 Responses to “JQuery Quirks”

    1. Gravatar Ian Clifton UNITED STATES Says: Reply to this comment

      Does

      $(":checkbox").attr("checked", false);

      work (no quotes around false)? The removeAttr method makes sense though )

      Posted using Mozilla Firefox Mozilla Firefox 2.0.0.12 on Ubuntu Linux Ubuntu Linux
    2. Gravatar Luke Maciak UNITED STATES Says: Reply to this comment

      Actually… That works. I’m dumb. LOL

      Posted using Mozilla Firefox Mozilla Firefox 2.0.0.12 on Ubuntu Linux Ubuntu Linux
    3. Gravatar Ian Clifton UNITED STATES Says: Reply to this comment

      Don’t worry, these damned computers get the best of us sometimes ) A coworker was struck by a similar issue today. He was creating a radio button with choices of Yes or No and values set to “1″ and “0″ respectively. When Django was processing them, it returned them both as True. Funny things, those non-empty strings, haha.

      Posted using Mozilla Firefox Mozilla Firefox 2.0.0.12 on Ubuntu Linux Ubuntu Linux

    Leave a Reply

    XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <pre lang=""> <em> <i> <strike> <strong>

    [Quote selected]