1

I have a jQuery code here:

jQuery(document).ready(function () {
    $("td").click(function (e) {
        var chk = $(this).closest("tr").find("input:checkbox").get(0);

        if (e.target != chk) {
            chk.checked = !chk.checked;
        }

        if ($(this).closest("tr").find("input:checkbox").is(":checked")) {
            $(this).closest("tr").css("font-weight", "bold");
        } else {
            $(this).closest("tr").css("font-weight", "");
        }
    });
});

As you can see I have a lot of checkboxes and on click on any td it's checked and adding css.

It works fine. What I want, is in the same form I also have a lot of select boxes.
When I try to choose something the function works.
I want to disable the function on click on the select box because I want to choose not to enable the checkbox.

2 Answers 2

1

You could just check if the target is a select or anything inside a select element, like options :

$(function () {
    $("td").on('click', function (e) {
        if (! $(e.target).closest('select').length ) {
           var tr  = $(this).closest("tr"),
               chk = tr.find("input[type='checkbox']").get(0);
           if (e.target != chk) {
               chk.checked = !chk.checked;
           }
           if ( tr.find("input[type='checkbox']").is(":checked")) {
               tr.css("font-weight", "bold");
           } else {
               tr.css("font-weight", "");
           }
        }
    });
});
Sign up to request clarification or add additional context in comments.

Comments

0

If I understand correctly, you want to test what the mouse clicked, and if it was the , then don't change the checkbox, correct?

You should be able to get that from the event's properties. For example:

if (!$('#' + e.target.id).hasClass('mySelectBox')){
  //your code...
}

There might be a more succinct way, I haven't tested, but try this:

if (e.target != 'select'){
  //your code..
}

Update: Yeah, what @adeneo said!

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.