Julian Dormon
Julian Dormon

Reputation: 1779

Cannot set checkbox to checked using jquery

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!

Upvotes: 0

Views: 1151

Answers (2)

Zeb Rawnsley
Zeb Rawnsley

Reputation: 2220

Change

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

To

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

Upvotes: 4

Mamuz
Mamuz

Reputation: 1730

try this:

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

Upvotes: 0

Related Questions