user1645434
user1645434

Reputation: 203

selection check of select Box

I'm getting stuck to check StartAll & StopAll

What I want to implement is that ->

1> on selecting All checkbox, all the features(except stopall) will be selected and disabled.

2>on selecting Stopall,all the features will be unselected and disabled.

3>B,D & E's value will be slected in the textbox.

Here's the jsfiddle link : http://jsfiddle.net/bigzer0/PKRVR/5/

<div align="center" id="checkboxes">
<input type="checkbox" name="startall" data-name="Startall" class="check" id="check" value="all"> All &nbsp;&nbsp;&nbsp;
<input type="checkbox" name="stopall" data-name="Stopall" class="check" id="check" value="stopall"> STOPall &nbsp;&nbsp;
<input type="checkbox" name="apple" data-name="Apple" class="check" id="check" value="a"> Apple &nbsp;&nbsp;&nbsp;
<input type="checkbox" name="ball" data-name="Ball" disabled="disabled" checked="checked"  id="check" value="b"> Ball
    &nbsp;&nbsp;&nbsp;
<input type="checkbox" name="cat" data-name="Cat" class="check" id="check" value="c"> Cat &nbsp;&nbsp;&nbsp;
<input type="checkbox" name="dog" data-name="Dog" checked="checked" disabled="disabled"  id="check" value="d"> Dog
    &nbsp;&nbsp;&nbsp;
<input type="checkbox" name="elephant" data-name="Elephant" disabled="disabled" checked="checked"  id="check" value="e">
    Elephant &nbsp;&nbsp;&nbsp;
</div>


<input type="text" name="policyName" id="policyName" title="Policy Name" class="cpolicyname" value="Start" readonly="readonly" >

<input type="text" name="features" title="Policy Features" class="cfeatures" id="features" readonly="readonly" size="60">

my (nightmare)jquery :

$(document).ready(function() {
    $('.check').click(function(){
        $("#policyName").val('Start');
        $("#features").val('');
                  $(".check").each(function(){
            if($(this).prop('checked')){

                $("#policyName").val($("#policyName").val() + $(this).val());    
                $("#features").val($("#features").val() + $(this).data('name'));
                }            
        });

     });
});

Any inputs will be appreciated and welcome.

Upvotes: 1

Views: 193

Answers (1)

androidavid
androidavid

Reputation: 1259

Why would you even do it like this? You dont even check which button was clicked. Give the checkboxes other than Stopall and Startall another class like "checkable"

$('.check').click(function(){
    if ($(this).attr('name')=='Stopall') {
        $('.checkable').attr('disabled',true).attr('checked',false);

    } else if ($(this).attr('name')=='Startall') {
        $('.checkable').attr('disabled',true).attr('checked',true);
    }
}

You can give multiple classes on css like this:

 <input type="checkbox" name="anything" class="class1 class2 class3" />

I think the rest is really at you ;)

Upvotes: 1

Related Questions