Checkbox - accessing via class selector seems to work once and not again.
Hello Group!
I originally thought this was a problem with jQueryUI but decided to try with out the UI and found its more basic.
If I create groups of checkboxes and modify them via the class selector, I get some strange results. I have two groups of checkboxes all with unique id's and each group with its own class. I would like each group to reflect its checked value. The demo code below shows what I've done. When you click one "Test check A" (group A), then all of the A group checks will check. Likewise with uncheck. However do it again, it does not work. This is the same with group B.
One note: The unchecking of the group seems to work consistently. Tried this on chrome/IE
Any ideas? Did I do something wrong?
Code:
-
- <!doctype html>
- <html lang="en">
- <head>
- <meta charset="utf-8">
- <title>jQuery UI Button - Default functionality</title>
- <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
- <script src="//code.jquery.com/jquery-1.10.2.js"></script>
- <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
- <script>
- function buttonTest(elm) {
- var parts = elm.id.split("-");
- // Get class
- var cls = ".test-check" + parts[2];
- $(cls).attr("checked", elm.checked);
- }
- </script>
- </head>
- <body>
-
- <input type="checkbox" class="test-check1" id="test-check-1-a" onclick="buttonTest(this)" /><label for="test-check-1-a">Test check A</label>
- <input type="checkbox" class="test-check2" id="test-check-2-a" onclick="buttonTest(this)" /><label for="test-check-2-a">Test check B</label>
- <br />
- <input type="checkbox" class="test-check1" id="test-check-1-b" onclick="buttonTest(this)" /><label for="test-check-1-b">Test check A</label>
- <input type="checkbox" class="test-check2" id="test-check-2-b" onclick="buttonTest(this)" /><label for="test-check-2-b">Test check B</label>
- </body>
- </html>
Thanks in advance !
Monte---