Symfony
Symfony

Reputation: 2439

Why javascript not working on checkbox?

I have two groups of checboxes, when i click on first group of checkboxes the second group of checkbox should be unchecked. but the second checkbox unchecked when I click on first checkbox two times. here is my javascript code

 <script>
            function uncheck0(){
                for(var ii=1; ii<=3; ii++){
                    if(document.getElementById("q6_"+ii).checked==true){
                       document.getElementById("q6_"+ii).checked=false;
                    }
                }
            }
            function uncheck(id, from, to){
                for(var ii=from; ii<=to; ii++){
                    if(document.getElementById(id+ii).checked==true){
                       document.getElementById(id+ii).checked=false;
                    }
                }
            }
        </script>

and here is my html code first group has radio-buttons and second has checkboxes

<p>IF YES: When will the review begin?</p>
            <label  for="q6"><input type="radio" class="styled" value="Within the next 12 months" name="q6[]" id="q6_1">Within the next 12 months</label>
            <label  for="q6"><input type="radio" class="styled" value="Within the next 24 months" name="q6[]" id="q6_2">Within the next 24 months</label>
            <label  for="q6"><input type="radio" class="styled" value="We are currently doing it" name="q6[]" id="q6_3">We are currently doing it</label><br>
        <br>
        <p>IF NO: Why Not</p>
            <label onclick="uncheck('q6_',1,3);" for="q6"><input type="checkbox" class="styled" value="Lack of budget" name="q6[]" id="q6_4">Lack of budget</label>
            <label onclick="uncheck('q6_',1,3);" for="q6"><input type="checkbox" class="styled" value="Lack of resource" name="q6[]" id="q6_5">Lack of resource</label>
            <label onclick="uncheck('q6_',1,3);" for="q6"><input type="checkbox" class="styled" value="Do not see the need" name="q6[]" id="q6_6">Do not see the need</label>
            <label onclick="uncheck('q6_',1,3);" for="q6"><input type="checkbox" class="styled" value="Lack of know how/expertise" name="q6[]" id="q6_7">Lack of know how/expertise</label>
            <label onclick="uncheck('q6_',1,3);" for="q6"><input type="checkbox" class="styled" value="Complexity" name="q6[]" id="q6_8">Complexity</label>
            <label onclick="uncheck('q6_',1,3);" for="q6"><input type="checkbox" class="styled" value="Contractual obligations" name="q6[]" id="q6_9">Contractual obligations</label>
            <label onclick="uncheck('q6_',1,3);" for="q5"><input type="checkbox" class="styled" value="Other" name="q6[]" id="q6_10">Other </label>
            <label onclick="uncheck('q6_',1,3);" for="q5a">Please state</label><input type="text" value="" id="q6a" name="q6a">

I have use a javascript file "custom-form-element.js" for checkbox and radio styling

Upvotes: 0

Views: 1845

Answers (2)

Carlo
Carlo

Reputation: 1714

Move the onclick="uncheck('q6_',1,3);" from the labels to the input boxes.
Your final code should look something like:

<label for="q6">
    <input type="checkbox" onclick="uncheck('q6',1,3);" class="styled" value="Lack of budget" name="q6[]" id="q6_1">Lack of budget</label>
<label onclick="uncheck('q6_',1,3);" for="q6_2"><input type="checkbox" class="styled" value="Lack of resource" name="q6[]" id="q6">Lack of resource</label>

Upvotes: 1

killebytes
killebytes

Reputation: 980

You need to have a default "checked" value

 <input type="checkbox" checked="true">

Otherwise, your code will think it is not checked then set the value as "checked" then the 2nd click will "unchecked" it.

Upvotes: 1

Related Questions