0

Here is the HTML Sample dynamically generated from c#.net:-

<ul class="multiselect-checkboxes clearfix horizontal" id="chkMC"><li onclick="setMcCb(event);"><span class="QRadio"><input type="checkbox" name="ctl03$ctl05$CheckboxChoices0-931336440" id="ctl03_ctl05_CheckboxChoices0-931336440"><label for="ctl03_ctl05_CheckboxChoices0-931336440">1-2 Years</label></span></li><li onclick="setMcCb(event);"><span class="QRadio"><input type="checkbox" name="ctl03$ctl05$CheckboxChoices1-1671570667" id="ctl03_ctl05_CheckboxChoices1-1671570667"><label for="ctl03_ctl05_CheckboxChoices1-1671570667">5-10 Years</label></span></li><li onclick="setMcCb(event);"><span class="QRadio"><input type="checkbox" name="ctl03$ctl05$CheckboxChoices2679786207" id="ctl03_ctl05_CheckboxChoices2679786207"><label for="ctl03_ctl05_CheckboxChoices2679786207">10-12 Years</label></span></li><li onclick="setMcCb(event);"><span class="QRadio"><input type="checkbox" name="ctl03$ctl05$CheckboxChoices3-977631492" id="ctl03_ctl05_CheckboxChoices3-977631492"><label for="ctl03_ctl05_CheckboxChoices3-977631492">12-15 Years</label></span></li><li onclick="setMcCb(event);"><span class="QRadio"><input type="checkbox" name="ctl03$ctl05$CheckboxChoices41547758987" id="ctl03_ctl05_CheckboxChoices41547758987"><label for="ctl03_ctl05_CheckboxChoices41547758987">15-20 Years</label></span></li><li onclick="setMcCb(event);"><span class="QRadio"><input type="checkbox" name="ctl03$ctl05$CheckboxChoices5-1658326960" id="ctl03_ctl05_CheckboxChoices5-1658326960"><label for="ctl03_ctl05_CheckboxChoices5-1658326960">20-21 Years</label></span></li><li onclick="setMcCb(event);"><span class="QRadio"><input type="checkbox" name="ctl03$ctl05$CheckboxChoices6-100787876" id="ctl03_ctl05_CheckboxChoices6-100787876"><label for="ctl03_ctl05_CheckboxChoices6-100787876">22-23 Years</label></span></li><li onclick="setMcCb(event);"><span class="QRadio"><input type="checkbox" name="ctl03$ctl05$CheckboxChoices7122075492" id="ctl03_ctl05_CheckboxChoices7122075492"><label for="ctl03_ctl05_CheckboxChoices7122075492">23-24 Years</label></span></li><li onclick="setMcCb(event);"><span class="QRadio"><input type="checkbox" name="ctl03$ctl05$CheckboxChoices8-1742375481" id="ctl03_ctl05_CheckboxChoices8-1742375481"><label for="ctl03_ctl05_CheckboxChoices8-1742375481">More than that</label></span></li><li><span class="QRadio"><input type="checkbox" name="ctl03$ctl05$CheckboxChoicesOther" id="ctl03_ctl05_CheckboxChoicesOther"><label for="ctl03_ctl05_CheckboxChoicesOther">other</label></span>&nbsp;&nbsp;<input type="text" style="width: 150px;" class="QTextBox" id="ctl03_ctl05_otherText" name="ctl03$ctl05$otherText"></li><li><span class="QRadio"><input type="checkbox" name="ctl03$ctl05$CheckboxChoicesNA" id="ctl03_ctl05_CheckboxChoicesNA"><label for="ctl03_ctl05_CheckboxChoicesNA">plplpl</label></span></li></ul>

JS Method:-

function setMcCb(e) {  
                if (!e) var e = window.event;                             
                var tg = (window.event) ? e.srcElement : e.target;  //FF & IE.
                var j$chk = j$(tg).closest('li').find(':checkbox');

                if ( j$chk[0] != tg)                        
                    j$chk.attr('checked', !j$chk.attr('checked'));
                }

*Purpose of the JS Method:- I created this javascript when anyone clicks the checkbox, label or the li tag, the checkbox should toggle. *

*My Problem:- When i click the label element, the js is not working. The toggle is not happening. Please help me refine the js code. *

I made it work the following code:-

 if (!e) var e = window.event;                             
                    var tg = (window.event) ? e.srcElement : e.target;  //FF & IE.                    
                    //Label click does not toggle the cbx with this JS, so the below processing is skipped by returning false.
                    if (tg.tagName.toUpperCase() == 'LABEL')
                        return false;

                    var j$chk = j$(tg).closest('li').find(':checkbox');
                    if ( j$chk[0] != tg){                        
                         if(j$chk.attr('checked'))
                            j$chk.removeAttr('checked');
                         else 
                            j$chk.attr('checked', 'checked');
                        }

2 Answers 2

3

Your problem is in the following line

j$chk.attr('checked', !j$chk.attr('checked'));

The call to !j$chk.attr('checked') will return a boolean value, which you're then assigning to checked. You should try:

if(j$chk.attr('checked')) {
  j$chk.removeAttr('checked');
} else {
  j$chk.attr('checked', 'checked');
}
Sign up to request clarification or add additional context in comments.

4 Comments

Guys, Now the click on the label is not doing the toggle with the above code. I want to make the toggle work on all the three elements checkbox, label and the LI item.
One thing i observed is that the code in the else part ( j$chk.attr('checked', 'checked'); ) does not get into effect in the case of Label. And this is weird.
Another thing i found, the checkbox does not even get unchecked when the label is clicked to set it unchecked.
could you format that code in your original post? it's pretty difficult to read.
1

Instead try

 if ( j$chk[0] != tg)    
     if(!j$chk.attr('checked')){
         j$chk.attr('checked', 'checked');
     }else {
         j$chk.removeAttr('checked');
     }
 }

4 Comments

wouldn't work. you're only setting the checked attribute if the box is checked.
that is what you want? checking checkbox on click on label if its unchecked and unchecking it if checkbox is already checked. Right?
yes, but your code will only set the 'checked' attribute when it already has the 'checked' attribute (meaning it is already checked!)
Yes you are right, I forgot to add ! in if condition. Thanks.

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.