Milan Bastola
Milan Bastola

Reputation: 304

How to select checkbox name with array

I am trying to disable other checkboxes with same name if one of the checkbox is selected. Here is my code:

<div id="selectSessionEvening" class="sessionEvening">
    <p style="font-weight:bold;">Evening Session</p>
    <div class="form-check">
        <input class="form-check-input" type="checkbox" value="4:00 PM - 5:00 PM" id="four-five" onFocus="startCalc();" onBlur="stopCalc();" name="sessionTime">
        <label class="form-check-label" for="four-five">4:00 PM - 5:00 PM</label>
    </div>
    <div class="form-check">
        <input class="form-check-input" type="checkbox" value="5:00 PM - 6:00 PM" id="five-six" onFocus="startCalc();" onBlur="stopCalc();" name="sessionTime">
        <label class="form-check-label" for="five-six">5:00 PM - 6:00 PM</label>
    </div>
    <div class="form-check">
        <input class="form-check-input" type="checkbox" value="6:00 PM - 7:00 PM" id="six-seven" onFocus="startCalc();" onBlur="stopCalc();" name="sessionTime">
        <label class="form-check-label" for="six-seven">6:00 PM - 7:00 PM</label>
    </div>
    <div class="form-check">
        <input class="form-check-input" type="checkbox" value="7:00 PM - 8:00 PM" id="seven-eight" onFocus="startCalc();" onBlur="stopCalc();" name="sessionTime">
        <label class="form-check-label" for="seven-eight">7:00 PM - 8:00 PM</label>
    </div>
</div>

Here is the JS:

$('input[name=sessionTime]').change(function(){
    if($(this).is(':checked')){
        $('input[name=sessionTime]').attr('disabled',true);
        $(this).attr('disabled', false);
        console.log('disabled');
    } else {
        $('input[name=sessionTime]').attr('disabled', false);
        console.log('not disabled');
    }
});

The Code is working fine this way. Now, What I need to do is I need to pass each checked checkbox in next form so I needed to write element name as selectTime[] After doing this My JS is not working now. I have searched for the solution but couldn't get working for me. Do you have any solution for this?

Here is the updated code:

<div id="selectSessionEvening" class="sessionEvening">
                      <p style="font-weight:bold;">Evening Session</p>
                      <div class="form-check">
                        <input class="form-check-input" type="checkbox" value="4:00 PM - 5:00 PM" id="four-five" onFocus="startCalc();" onBlur="stopCalc();" name="sessionTime[]">
                        <label class="form-check-label" for="four-five">4:00 PM - 5:00 PM</label>
                      </div>
                      <div class="form-check">
                        <input class="form-check-input" type="checkbox" value="5:00 PM - 6:00 PM" id="five-six" onFocus="startCalc();" onBlur="stopCalc();" name="sessionTime[]">
                        <label class="form-check-label" for="five-six">5:00 PM - 6:00 PM</label>
                      </div>
                      <div class="form-check">
                        <input class="form-check-input" type="checkbox" value="6:00 PM - 7:00 PM" id="six-seven" onFocus="startCalc();" onBlur="stopCalc();" name="sessionTime[]">
                        <label class="form-check-label" for="six-seven">6:00 PM - 7:00 PM</label>
                      </div>
                      <div class="form-check">
                        <input class="form-check-input" type="checkbox" value="7:00 PM - 8:00 PM" id="seven-eight" onFocus="startCalc();" onBlur="stopCalc();" name="sessionTime[]">
                        <label class="form-check-label" for="seven-eight">7:00 PM - 8:00 PM</label>
                      </div>
                    </div>

Here is the working jsfiddle: https://jsfiddle.net/humanware/y1c330f8/4/

UPDATE: I tried doing this in JS:

$('input[name=sessionTime[]]').change(function(){
    if($(this).is(':checked')){
        $('input[name=sessionTime[]]').attr('disabled',true);
        $(this).attr('disabled', false);
        console.log('disabled');
    } else {
        $('input[name=sessionTime[]]').attr('disabled', false);
        console.log('not disabled');
    }
});

But Got This Error: enter image description here

Upvotes: 0

Views: 345

Answers (1)

Bhushan Kawadkar
Bhushan Kawadkar

Reputation: 28513

Try this: User quotes around name when there is special characters in the name.

I have modified your code little here - use this

$('input[name="sessionTime[]"]').change(function(){
  var checked = $(this).is(':checked');
    if(checked){
        $('input[name="sessionTime[]"]').not(this).prop('disabled',checked);
    } else {
        $('input[name="sessionTime[]"]').prop('disabled', checked);
        console.log('not disabled');
    }
});

JSFiddle

Upvotes: 1

Related Questions