views:

364

answers:

1

Hey guys, I'm trying to make a small addition to a web app I use. Right now I am trying to check all of the checkboxes on the page which have class .checkBox (In case it is needed to differentiate/select). The checkboxes are descendants of divs of class .someClass, it's just that there are many divs which have that class. I want to check the boxes which are descendants of divs whose class is only .someClass.

In other words:

<!-- Check this box -->
<div class="someClass"> [...] <input type="checkbox" class="checkBox" /></div>

<!-- But not this one -->
<div class="someClass otherClasses lolWut"> [...] <input type="checkbox" class="checkBox" /></div>

Remember, the checkboxes aren't direct children, but descendants.

Thanks, I would appreciate any help :)

+3  A: 

Here you go:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

    <title>Check the boxes</title>
    <script src="prototype.js" type="text/javascript"></script>

    <script type="text/javascript">
    function checkThem() {
      // get all the .someClass divs
      $$(".someClass").each(function(item) {
        // filter out the ones that have additional classes 
        if(item.className == 'someClass') {
          // get the .checkBox descendants
          item.select('.checkBox').each(function(checkbox) {
            // check them
            checkbox.checked = true;
          });
        }
      });   
    }
    </script>

</head>

<body>

  <!-- Check this box -->
  <div class="someClass">Check: <input type="checkbox" class="checkBox" /><input type="checkbox" class="checkBox" /></div>

  <!-- But not this one -->
  <div class="someClass otherClasses lolWut">Don't check: <input type="checkbox" class="checkBox" /></div>

  <!-- Check this box -->
  <div class="someClass">Check: <input type="checkbox" class="checkBox" /><input type="checkbox" class="checkBox" /></div>

  <br /><br />

  <a href="#" onclick="checkThem(); return false;">Check them.</a>

</body>
</html>
No Surprises
I tested your code in a separate html file and it worked! However, I'm having trouble using it on the project I'm using. I keep getting an error in Firebug "An invalid or illegal string was specified" code: "12" followed by "results = $A(root.querySelectorAll(e)).map(Element.extend);\n", and nothing happens (no boxes get checked). The checkboxes are descendants, not direct children, is that why? I am using prototype.js 1.6.1
Jorge Israel Peña
It doesn't matter that they're descendants (I tested by nesting the checkboxes in some other elements). I also was testing with prototype.js 1.6.1. What are the actual class names in question? Since those are the only strings, that might be source of the problem (for example, if they're invalid classnames).
No Surprises
Actually, disregard the other message. I had upgraded prototype to prototype 1.6.1 (just the js file), and for some reason just now I figured I'd revert to see if that'd solve the problem, and it did. Maybe this web app is caching something, I don't know. I'm now running on 1.6.0.2, but at least it works. I'll try to figure out why it didn't work with 1.6.1 later. In the meantime, thanks for your solution, works wonderfully.
Jorge Israel Peña
You're welcome!
No Surprises