Javascript: enable checkbox list when checkbox is checked (using prototype)

I am using jquery for this, but now I need to do it with Prototype and Im a bit confused due to lack of documentation

I have 2 lists of checkboxes

First list:

Check box 1
Check box 2

      

Second list:

Check box x
check box y
check box z

      

I need some JS code using a prototype to work like this: Second list, remains disabled unless I check one of the checkboxes of the first list.

Any suggestions?

+2


a source to share


2 answers


Here's the JavaScript code:

Event.observe(window, 'load', function(){
    // for all items in the group_first class
    $$('.group_first').each(function(chk1){
        // watch for clicks
        chk1.observe('click', function(evt){
            // count how many of group_first
            // are checked, true if any are checked
            var doEnable = ($$('.group_first:checked').length > 0);
            // for each in group_second, enable the
            // checkbox, and remove the cssDisabled class
            $$('.group_second').each(function(item){
                if (doEnable) {
                    item.enable().up('label').removeClassName('cssDisabled');
                } else {
                    item.disable().up('label').addClassName('cssDisabled');
                }
            });
        });
    });
});

      

Based on this HTML:

<fieldset>
    <legend>First Group</legend>
    <label><input type="checkbox" value="1"
        class="group_first" />Check box 1</label><br />
    <label><input type="checkbox" value="2"
        class="group_first" />Check box 2</label>
</fieldset>


<fieldset>
    <legend>Second Group</legend>
    <label class="cssDisabled"><input type="checkbox" value="x"
        class="group_second" disabled="disabled" />Check box x</label><br />
    <label class="cssDisabled"><input type="checkbox" value="y"
        class="group_second" disabled="disabled" />Check box y</label><br />
    <label class="cssDisabled"><input type="checkbox" value="z"
        class="group_second" disabled="disabled" />Check box z</label>
</fieldset>

      

Add this CSS:



.cssDisabled { color: #ccc; }

      

The documentation for Prototype is very good. Here are the methods I am using:

For those of you wondering how this can be done in jQuery:

jQuery(document).ready(function(){ 
   $('.group_first').bind('click', function(){
        var doEnable = ($('.group_first:checked').length > 0);
        $('.group_second').each(function(){
            if (doEnable) {
                $(this).attr('disabled', null);
                $(this).parents('label').removeClass('cssDisabled');
            } else {
                $(this).attr('disabled', 'disabled');
                $(this).parents('label').addClass('cssDisabled');
            }
        });
    });
});

      

+4


a source


This is test code. Ive build and check the 1st group checkboxes do not include the checkboxes of group 2.



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>toggle disabled</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script src="prototype.js" type="text/javascript"> 
<script type="text/javascript">
var checkboxes = $$('input[name="group1"]');
checkboxes.each(function(s) {
    s.observe('click', function() {
        if(this.checked) {
            $$('input[name="group2"]').each(function(s) {
                s.enable();
            });
        } else {
            if(!$$('input[name="group2"][checked="checked"]').length) {
                alert('nothing checked');
                $$('input[name="group2"]').each(function(s) {
                    s.disable();
                });
            }
        }
    });
});
</script>

</head>
<body>
    <div>
        <label> Group 1 </label>
        <input type="checkbox" name="group1" value="test"/>
        <input type="checkbox" name="group1" value="test"/>
        <input type="checkbox" name="group1" value="test"/>
    </div>
    <div>
        <label> Group 2 </label>    
        <input type="checkbox" name="group2" disabled="disabled"/>
        <input type="checkbox" name="group2" disabled="disabled"/>
        <input type="checkbox" name="group2" disabled="disabled"/>
    </div>
</body>
</html>

      

0


a source







All Articles