Question

I'm trying to create a single multiple choice question.

In the example below, I would like option 1 and 3 to be the correct answer. When the submit button is clicked, i just wanted to run an if statment to see if the option 1 and 3 checkboxes are selected, and if so, toggle the correct1 div. Else if, option 1 and 3 are not selected, toggle the incorrect1 div.

in the Head tag I would have

<script type="text/javascript">
    function toggleDiv2(divId) {
    $("#"+divId).toggle("slow");
            }
</script>

Then in the body i would have:

<div class="control-group">

            <label class="control-label" for="optionsCheckboxList">Check your answers</label>
            <div class="controls">
              <label class="checkbox">
                <input type="checkbox" id="option1" name="optionsCheckboxList1" value="option1">
                <strong>Option One</strong> this is a correct answer. Select this one.
              </label>
              <label class="checkbox">
                <input type="checkbox" id="option2" name="optionsCheckboxList2" value="option2">
                <strong>Option two</strong>  this is an incorrect answer. Do not select this one.
              </label>
              <label class="checkbox">
                <input type="checkbox" id="option3" name="optionsCheckboxList3" value="option3">
                <strong>Option three</strong>  This is a correct answer. Select this one.
              </label>
              <p class="help-block"><strong>Hint:</strong> Add a hint if you would like. This is optional.</p>
          <br>


        <a href="javascript:toggleDiv2('correct1');" class="btn btn-primary">Submit</a>

          <br><br>

            <div id="correct1" class="alert alert-success" style="display: none">
            Well Done! You correctly answered this question!
            </div><!-- end correct1 -->

            <div id="incorrect1" class="alert alert-error" style="display: none">
            Oh snap! Please try again.
            </div><!-- end incorrect1 -->
            </div><!-- end control-group -->

Not sure if this matters or not, but this is the jquery the page is linking to:

<script src="html/assets/js/jquery-ui-1_8_18custom/js/jquery-1.7.1.min.js" type="text/javascript"></script>
<script src="html/assets/js/jquery-ui-1_8_18custom/js/jquery-ui-1.8.18.custom.min.js" type="text/javascript" ></script>
Was it helpful?

Solution

This got it working...

<script type="text/javascript">
   function toggleDiv2() {
   if($("#option1").is(':checked') && $("#option3").is(':checked'))
   $("#correct1").toggle("slow");
    else
   $("#incorrect1").toggle("slow");
   }
</script>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top