1

I have a JavaScript function in place for a multiple choice quiz:

If the answer is 'Yes' - (checked by a checkbox) - Then a sub-question will appear, but if the user clicks 'No' the sub-question won't appear.

JavaScript

   $(document).ready(function() {
  var par = $('#SQ1');
  $(par).hide();

  $('#Q1').click(function(e) {
      $(par).slideToggle('slow');

  });
});

    $(document).ready(function(){
      $('#Q1NO').click(function(){
         $('#SQ1').hide('slow');
      });
   })

Questions checkboxes

<div>
    <input type="checkbox" id="Q1" name="chk[]" value="Yes">&nbsp;Yes</input>
<br>
    <input type="checkbox" id="Q1NO" name="chk[]" value="No">&nbsp;No</input>
</div>

Sub-Question checkboxes

  <div id="SQ1">
<div>
    <input type="checkbox" name="chk[]" value="Yes 2" id="Q1R">&nbsp;Yes</input>
<br>
    <input type="checkbox" name="chk[]" value="No 2">&nbsp;No</input>
</div>
</div>

I am inputting the values from the checkboxes into a database and the problem is that if the user clicked 'Yes' and then selected a box from the sub-question, but then changed their mind and clicked 'No' on the primary question, the sub-question will disappear, but the box is still checked inside it and it gets added to the database - Which I don't want.

Is there a way to uncheck an item within a div, on the click of the 'No' from the first div?

Thanks for reading

5
  • element.checked = false;? Commented Dec 9, 2015 at 13:23
  • did you try with jQuery? $('chk').prop('checked', false); Commented Dec 9, 2015 at 13:24
  • No that hasnt worked Commented Dec 9, 2015 at 13:37
  • I'm guessing your code isn't complete, as I don't see an #SQ1 in your markup. Also, I'm guessing you should be using radio buttons not checkboxes for your use case... Commented Dec 9, 2015 at 13:58
  • I have some JQuery in place so only one checkbox can be selected at once, I didn't want to use radio buttons. I forgot to copy the SQ1 over, check my edit above Commented Dec 9, 2015 at 14:03

3 Answers 3

2

Uncheck all subquestions checkboxes:

$('#Q1NO').click(function(){
     $('#SQ1').hide('slow')
     .find('input:checkbox').prop('checked',false);
});
Sign up to request clarification or add additional context in comments.

Comments

2

First of all there either should be radio button for Yes/no or only one checkbox. There are several ways to achieve your requirement. I have posted below easiest one. See if it helps.

function showHideSub(isChecked){

if(isChecked)
{
$("#sub").show()
}
else
{
$("#sub").hide()
$("#sub input[type=checkbox]").removeAttr("checked");

}

}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<div>
    <input type="checkbox" id="Q1" name="chk[]" onclick="showHideSub(this.checked)" value="Yes">&nbsp;Yes / No </input>
<br>
   
</div>


<div id="sub" style="display:none">
    <input type="checkbox" name="chk[]" value="Yes 2" id="Q1R">&nbsp;Yes/No</input>

</div>

4 Comments

How could this possibly insert Yes or No into a database field when there is only one checkbox for two answers?
$('#' + id).is(":checked") that gets if the checkbox is checked. if its checked then yes else no
I don't think its good user experience to give two checkboxes for Yes & No. While designing screen generally we avoid lots of elements visible to user as an good user experience.
I don't think you understand what I'm implementing here, nor do I agree that it affects the user experience having two checkboxes for yes/no. Example of my form: Have you been to France? - Yes --- No (Yes is ticked) Did you enjoy it? - Yes --- No
1

Solution without loop as you can set properties on all query matches.

 $('#Q1NO').click(function() {
    par.hide('slow');
    par.find("input[type=checkbox]").prop('checked', false);
  });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.