3

In my current JQuery, I have an event that will either check or uncheck a checkbox if the user clicks on a row in a table. The problem with this is, if the user actually checks the checkbox, the jquery will fire on the checkbox event and either check/uncheck the box, but then the TR event will fire and then undo the checkbox value.

See an example here: http://jsfiddle.net/radi8/KYvCB/1/

I can disable the checkbox but then if the user tries to select the checkbox, the TR event will not trigger.

What I need is a method to disable the 'click' event of the checkbox but still allow the TR event to fire when the checkbox is selected.

var charges = {
    init: function() {
        // get the selected row checkbox
        //$('.charges').attr('disabled', true);
        $('.rowclick tr').click(function() {
            if ($(this).find('td input.charges:checkbox').is(':checked')) {
                $(this).find('td input.charges:checkbox').attr("checked", false);
            }
            else {
                $(this).find('td input.charges:checkbox').attr("checked", true);
            }
        });
    }
};
charges.init();
3
  • what exactly is wrong with your code? what's the result and what do you expect it to do? Commented Apr 24, 2012 at 14:07
  • this example you posted seems to work... what exactly is your question?? Commented Apr 24, 2012 at 14:07
  • JKirchartz: no it's not working, if you click on a checkbox, it will toggle twice Commented Apr 24, 2012 at 14:20

2 Answers 2

16

You need to check if the click event was fired on a checkbox or somewhere else. This needs less ressources than a second event handler for the checkbox with e.stopPropagation.

    $('.rowclick tr').click(function(e) {
        if($(e.target).closest('input[type="checkbox"]').length > 0){
            //Chechbox clicked
        }else{
            //Clicked somewhere else (-> your code)
            if ($(this).find('td input.charges:checkbox').is(':checked')) {
                $(this).find('td input.charges:checkbox').attr("checked", false);
            }
            else {
                $(this).find('td input.charges:checkbox').attr("checked", true);
            }
        }
    });

Working example: http://jsfiddle.net/KYvCB/5/

Sign up to request clarification or add additional context in comments.

4 Comments

What if there is another checkbox outside the table? ;]
This works perfectly! Thanks so much for the help. As always, you guys on this list are the best!
you're right, my fault... I think this is better: if($(e.target).is(':checkbox')){ //Chechbox clicked } jsfiddle.net/KYvCB/8
no problem radi8, glad to help you. And tarmaq: yes, it works in this case. you would have problems if the checkbox had child elements (impossible, i know). However, if it was a div element instead of a checkbox, you also need to check if the target is a child of the div.
2

you need to stop propagation of the click event:

var charges = {
    init: function() {
        $('.td input.charges:checkbox').on('click', function (e)
        {
            e.stopPropagation();
        })
        $('.rowclick tr').click(function() {
            if ($(this).find('td input.charges:checkbox').is(':checked')) {
                $(this).find('td input.charges:checkbox').attr("checked", false);
            }
            else {
                $(this).find('td input.charges:checkbox').attr("checked", true);
            }
        });
    }
};
charges.init();

http://jsfiddle.net/KYvCB/4/

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.