JQuery checkbox state is updated differently between group click() and $.each(obj.click())

Posted by teerapap on Stack Overflow See other posts from Stack Overflow or by teerapap
Published on 2010-05-31T06:59:23Z Indexed on 2010/05/31 7:03 UTC
Read the original article Hit count: 249

Filed under:
|
|

The code below doesn't work in the same behavior. The sequence of click event and calling foo() is different. I want to know why they behave different sequence between call click() and iterate the objects before call click() on each.

<script type="text/javascript">

      function foo(obj){
        alert(obj.id+" ->"+obj.checked);
      }

      function clickAll(val){
        if (val) {
          $(":checkbox").click();
        } else {
          $(":checkbox").each(function(i,obj){
              obj.click();
              });
        }
      }
    </script> 
  </head>
<body>
<input type="checkbox" id="check1" onclick="foo(this)" /> a
<input type="checkbox" id="check2" onclick="foo(this)" /> b
<input type="checkbox" id="check3" onclick="foo(this)" /> c
<input type="button" onclick="clickAll(true)" value="click all" />
<input type="button" onclick="clickAll(false)" value="click all each" />
</body>

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about events