Brian Seifert
Brian Seifert

Reputation: 109

how to check for 2 conditions within a hover function

I have a quiz page that asks a question and the user is to check 3 appropriate checkboxes as his or her answer (out of a list of 6 checkboxes). Then the user is to check his/her answers by hovering over the answer button ('#check-answer-btn')

If the user hovers over the $('#check-answer-btn') with less than 3 checkboxes checked, I want a certain alert $('.alert-box2') to fadeIn.

So my question is, can I have two conditionals within one hover function? I'm posting the code I have tried to no avail. Can someone help me find a solution? Thanks.

       $('#check-answer-btn').hover(function(e){
            var checkboxes = $('.chbx-25b input[type="checkbox"]:checked').length;
            if (checkboxes < 3) {
                $('.alert-box2').fadeIn(200);

            },  $('.alert-box2').stop().fadeOut(200);

            else if (checkboxes >= 3) {
                $('#checkbox-fadebox').fadeIn(200);
            },  $('#checkbox-fadebox').stop().fadeOut(200);
        );

I thought the HTML might be too much but here it is:

<p style="width:675px; margin:15px 0 20px 0;">Please choose the best answer for each of the following questions. <br>After you've selected your answer, roll over the <span class="bold">Check Answer</span> button.</p>

        <div class="quiz-25a question1 inline vertical" style="margin:15px 0 0 0;">

            <p class="bold float-left">1.</p><span class="bold float-left" style=" width:640px; margin:0 0 10px 10px;">Indicators are why the claim rep referred this questionable claim to you as an investigator.</span>
            <div class="clear"></div>

            <input id="quiz-25a-1a" type="radio" name="quiz-25a-1a" value="a"><p class="float-left" style="margin:0 12px;">a.</p><span>True</span>
            <div class="clear"></div>

            <input id="quiz-25a-1b" type="radio" name="quiz-25a-1b" value="b"><p class="float-left" style="margin:0 12px;">b.</p><span>False</span>

            <div class="clear"></div>

            <button id="answer-btn1" class="answer-btn"></button>

        </div><!-- end quiz-25a question1 inline vertical -->





        <form class="chbx-25a inline vertical" style="margin:15px 0 0 0;">

        <p class="bold float-left">2.</p><span class="bold float-left" style=" width:640px; margin:0 0 10px 10px;">Identify 3 reasons included in the following list that are reasons for creating a vehicle theft insurance fraud scheme.</span>
            <div class="clear"></div>

            <label for="chbx-25a-2-1"><input id="chbx-25a-2-1" type="checkbox"><span class="bold float-left">A.</span><span>There is no work number for either Bob or Jane Field’s in the claim file.</span></label>
            <label for="chbx-25a-2-2"><input id="chbx-25a-2-2" type="checkbox"><span class="bold float-left">B.</span><span>The vehicle had 8400 miles on it at the time of the theft.</span></label>
            <label for="chbx-25a-2-3"><input id="chbx-25a-2-3" type="checkbox"><span class="bold float-left">C.</span><span>The theft happened in the middle of the day, downtown with no witnesses.</span></label>
            <label for="chbx-25a-2-4"><input id="chbx-25a-2-4" type="checkbox"><span class="bold float-left">D.</span><span>Mrs. Fields parked the car one door down from Betty’s.</span></label>
            <label for="chbx-25a-2-5"><input id="chbx-25a-2-5" type="checkbox"><span class="bold float-left">E.</span><span>The vehicle had a premium sound system.</span></label>
            <label for="chbx-25a-2-6"><input id="chbx-25a-2-6" type="checkbox"><span class="bold float-left">F.</span><span>The vehicle had an anti-theft alarm that no one heard.</span></label>

        </form><!-- end chbx-25a inline vertical -->

        <button id="check-answer-btn" class="check-answers-btn"></button>

        <p style="margin-top:15px;">Click NEXT to continue.</p>

        <div id="answer1" class="fade-box absolute" style="width:340px; height:105px; top:150px; left:270px;">
            <p class="answer-title bold">The correct answer is a, true.</p>
            <p class="font-size14">Indicators of a questionable claim are inconsistencies in a claimant’s story, they don’t make sense in the over-all big picture.  As these indicators mount up, it becomes more important to investigate the claim.</p>
        </div><!-- end answer1 -->


        <div id="checkbox-fadebox" class="fade-box absolute" style="text-align:center; width:410px; height:470px; top:65px; left:235px;">

            <p class="bold italic" style="font-size:16rpx; margin-top:5px;">The following indicators of vehicle theft insurance fraud apply to this case.</p>

            <img src="img/25b-chbx-ans.gif" alt="" style="margin:12px 0 0 0;"/>

        </div><!-- end checkbox-fadebox -->

        <div class="alert-box absolute">

            <p>Please select the best answer(s)<br> for <span style="text-decoration:underline;">each</span> of the following questions.</p>

            <img class="OK-btn absolute pointer" style="display:block; bottom:6px; right:6px;" src="img/OK-btn.gif" />

        </div>

        <div class="alert-box2 absolute">

            <p>Please identify 3 reasons for creating a vehicle theft insurance fraud scheme.</p>

        </div>

Upvotes: 0

Views: 72

Answers (1)

Brian Seifert
Brian Seifert

Reputation: 109

I found my answer by discovering errors in my own syntax and breaking the conditionals into two separate ones. I realized that the value I assigned to my variable "checkboxes" was different than the one found in my HTML (.chbx-25b and .chbx-25a). Also, the first curly braces used after each of the "ifs" are incorrect. I have posted the successful block of code below. Thanks to all who attempted to help!

       $('#check-answer-btn').hover(function(){
            var checkboxes = $('.chbx-25b input[type="checkbox"]:checked').length;

            if(checkboxes === 3)
                $('#checkbox-fadebox').fadeIn();
            }, function(){
                $('#checkbox-fadebox').stop().fadeOut();
        });

        $('#check-answer-btn').hover(function(){
            var checkboxes = $('.chbx-25b input[type="checkbox"]:checked').length;

            if(checkboxes!== 3)
                $('.alert-box25b').fadeIn();
            }, function(){
                $('.alert-box25b').stop().fadeOut();
        });

Upvotes: 1

Related Questions