0

This jQuery function is always returning true in alert(isChecked), even when unchecked:
pls note .accrualCheckbox is the class name of Div , not the class that is set for check box. I removed the code if ($(this).prop('checked', 'checked')), now it is always returning as 'false'

$('.accrualCheckbox').click(function () {
    var isChecked = $(this).attr('checked') ? true : false;
    alert($(this).attr('checked'));
    alert(isChecked);
    if ($(this).prop('checked', 'checked')) {
        var parentDiv = $(this).parents('.searchLine');
        $(parentDiv).css('border', '1px solid red');

        //Checkbox
        $(parentDiv).find("input[type=text]").each(function () {
            $(this).val('B');
            $(this).prop('disabled', true);
        });
    }
});
});

I kept the check box in the div:

<div class="accrualCheckbox">
    @Html.CheckBox("chkSalesAndMarketing")
</div>   
8
  • What does the last }); correspond to? I'm confused why it's there. Commented Aug 21, 2013 at 18:02
  • @phileaton I rolled back your edit because it's not a "fix", it's a matter of personal preference. The way he had it is just as much of a standard as what you changed it to. It's his post, so it should be left in his preferred style. Commented Aug 21, 2013 at 18:08
  • If anything needs to be fixed, it's the apparent extra final closing }); at the end of the first code block. @user2543573, why is that there? Commented Aug 21, 2013 at 18:09
  • @AdiInbar probably because the code is inside the document ready handler and he accidentally copied the closing along with the function he wanted to post Commented Aug 21, 2013 at 18:12
  • @koala_dev Yes, probably, but I asked rather than deleting it because I think it's generally a bad idea to correct code in an edit, because it's possible that what you're fixing might have something to do with the problem, or might have a purpose you weren't aware of. No, I don't think either one is the case here, but as a rule I never edit code in a question beyond fixing formatting issues without checking with the poster first. If you monkey around with the posted code, you risk changing the question. Commented Aug 21, 2013 at 18:17

2 Answers 2

3

You need to use prop() instead of attr() or you can also do:

var isChecked = $(this).is(':checked');

But this if ($(this).prop('checked', 'checked')) will always return true since you're setting the value to checked inside the condition

UPDATE I assume this is what you want to do:

$('.accrualCheckbox :checkbox').click(function () {
    if ($(this).is(':checked')) {  // or $(this).prop('checked');
        var parentDiv = $(this).closest('.searchLine');
        $(parentDiv).css('border', '1px solid red');
        //Checkbox
        $(parentDiv).find(":text").each(function () {
            $(this).val('B');
            $(this).prop('disabled', true);
        });
    }
});
Sign up to request clarification or add additional context in comments.

4 Comments

pls note .accrualCheckbox is the class name of Div , not the class that is set for check box. I removed the code if ($(this).prop('checked', 'checked')), now it is always returning as 'false'
@user2543573 please add that detail to your question, and if you just want to handle the click on the checkbox just update the selector for the click handler like this $('.accrualCheckbox :checkbox').click(function...
thanks, it is working now by ur change.i update this as Answer.
i added that details to questuion also.
1

Use $(this).prop('checked') instead of attr().

See the attributes vs. properties section here: http://api.jquery.com/prop/

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.