0

I have the following javascript function. A the top of the function, I am able to detect if a checkbox is checked using $(elem).is(':checked'). Later in the function I want to wire up an onclick event in a modal window such that it will checkmark the elem's checkbox, but this does not seem to work.

Here is the function:

function toggleProductChkBx(elem,id)
    {
        if ($(elem).is(':checked')) {

        } else {

            $('#clearProductModal').on('show', function () {
                removeBtn = $(this).find('.danger');
                removeBtn.click(function () { clearProduct(id) });

                cancelBtn = $(this).find('.secondary');

                //THIS IS THE LINE THAT IS NOT WORKING
                cancelBtn.click(function () { $(elem).attr("checked", "true"); });

            })
            .modal({ backdrop: true });
        }
    }

Thanks for the help!

4
  • 1
    possible duplicate of How do I check a checkbox with jQuery or JavaScript? Commented May 7, 2013 at 0:03
  • What version of jquery are you using? Commented May 7, 2013 at 0:14
  • Thanks. I'm using 1.9.1 Commented May 7, 2013 at 0:28
  • Also tried $(elem).checked(true); - no love. Commented May 7, 2013 at 0:30

2 Answers 2

4

Change

cancelBtn.click(function () { $(elem).attr("checked", "true"); });

To

cancelBtn.click(function () { $(elem).prop("checked", true); });

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

1 Comment

.prop('checked', true) would be better.
0

try this:

$(elem).attr("checked", true);

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.