1

I have a checkbox ".invalidDropChk" and when I click on it I want its child checkboxes to get clicked. That part is working fine. I have a "All" checkbox too. I want to check it if the other checkbox in the page ".invalidOffensive" is checked too. But the problem is that it is entering in both if and else part in the end and my checkbox "All" gets checked. Here is the code :

$('.invalidDropChk').live('click',function(){   
        if($(this).is(':checked')){
            $(this).next().find('input[type="checkbox"]').attr('checked',true);         
            $(this).attr('checked',true);
            var rad1 = document.getElementById("invalidDropChk");
            if(rad1 != null && rad1.attributes['checked'])
                rad1.checked = true;

        }
        else
        {
            $(this).next().find('input[type="checkbox"]').attr('checked',false);
            $(this).attr('checked',false);
            var rad1 = document.getElementById("invalidDropChk");
            if(rad1 != null && rad1.attributes['checked'])
                rad1.checked = false;
        }

        if (($(this).is(':checked')) && ($('.invalidOffensive').is(':checked'))){
            $('.check-all ').prop('checked',true);          
        }
        else{
            $('.check-all ').prop('checked',false);
        }   

    });

The jsp page code is:

<ul class="invalidDrop">
                <c:choose>
                  <c:when test="${invalidDropForm.invalidStreetAddress==true && invalidDropForm.invalidSubName==true && invalidDropForm.derogatoryNames==true && invalidDropForm.businessNames==true && invalidDropForm.offensiveNames==true}">
                     <li><input type="checkbox" class="check-all" checked /> All</li>
                  </c:when>
                  <c:otherwise>
                     <li><input type="checkbox" class="check-all" /> All</li>
                  </c:otherwise>
                </c:choose>
                <li>
                    <c:choose>
                      <c:when test="${invalidDropForm.invalidStreetAddress==true && invalidDropForm.invalidSubName==true}">
                           <input type="checkbox" class="check-inside invalidDropChk" id="invalidDropChk" checked /> Invalid Name/Address
                      </c:when>
                      <c:otherwise>
                         <input type="checkbox" class="check-inside invalidDropChk" id="invalidDropChk" /> Invalid Name/Address
                      </c:otherwise>
                    </c:choose>
                    <ul class="inneritem">                      
                        <li><sf:checkbox path="invalidStreetAddress"/> Invalid Street Address</li>
                        <li><sf:checkbox path="invalidSubName" /> Invalid Sub Name</li>
                    </ul>
                </li>
                <li>
                    <c:choose>
                      <c:when test="${invalidDropForm.derogatoryNames==true && invalidDropForm.businessNames==true && invalidDropForm.offensiveNames==true}">
                           <input type="checkbox" class="check-inside invalidOffensive"  id="invalidOffensive" checked/> Offensive, Derogatory, Business Names
                      </c:when>
                      <c:otherwise>
                            <input type="checkbox" class="check-inside invalidOffensive" id="invalidOffensive"/> Offensive, Derogatory, Business Names
                      </c:otherwise>
                    </c:choose>
                    <ul class="inneritem">
                        <li><sf:checkbox path="derogatoryNames" /> Derogatory Names</li>
                        <li><sf:checkbox path="businessNames" /> Business Names</li>
                        <li><sf:checkbox path="offensiveNames" /> Offensive Names</li>
                    </ul>
                </li>
            </ul>
Deepika Sethi
  • 213
  • 1
  • 2
  • 10
  • Can you post your HTML as well? Also, do you have a minute to talk about [JSFiddle](http://www.jsfiddle.net)? – cr0ss May 30 '14 at 19:26
  • @cr0ss yes posted the jsp part of it... never used JSFiddle.. Please explain more.. – Deepika Sethi May 30 '14 at 19:30
  • I hope you realize that `.live()` is long since deprecated and even removed from recent versions of jQuery. The [dynamic form of `.on()`](http://stackoverflow.com/questions/8752321/jquery-live-vs-on-method-for-adding-a-click-event-after-loading-dynamic-ht/8752376#8752376) can replace it. – jfriend00 May 30 '14 at 19:33
  • Actually if I use .on everything stops working... – Deepika Sethi May 30 '14 at 19:38
  • 1
    @DeepikaSethi Sure. It's a website that you can post some code, include js libraries to show other people. It is very useful to help us understand your problem. – cr0ss May 30 '14 at 19:50
  • @jfriend00 : In my case I will use $('invalidDrop').on('click','.invalidDropChk',function() if I replace live() with all() right? – Deepika Sethi May 30 '14 at 19:51
  • @cr0ss : In JSFiddle it doesn't work fine... http://jsfiddle.net/deepikasethi/32rPW/1/ – Deepika Sethi May 30 '14 at 20:25

1 Answers1

0

in text: ... if the other checkbox in the page ".invalidOffensive" is not checked

in code: ... && ($('.invalidOffensive').is(':checked'))

pavel
  • 26,538
  • 10
  • 45
  • 61