0

I have checkbox which once it's property of clicked is true, I would like to check another checkbox automatically.

I managed to partially get it working by changing the other value of the checkbox to true whenever the other is clicked. But this isn't what I want. I'd like to be able to check when the 'TwoFChk' checkbox is clicked and also if the property of checked is true at the same time. How do I do this?

JQuery:

if ($('#TwoFChk').change(function () {
            if ($(this).prop('checked', true)) {
            alert("It's checked");
            $('#ConfirmEmailChk').prop('checked', true);
        }
        }));

My question is different, here's why:

I'm not just trying to check if a checkbox is checked. I can do that already. I need to detect when the value of checked changes, and if the value of checked is then true. I need to change the checked value of another checkbox is true. Again, I am not just trying to check if a checkbox is checked. I need to have an on click of the checkbox and if the checkbox checked value is true, then I need to check another checkbox.

Andrew Kilburn
  • 2,251
  • 6
  • 33
  • 65
  • You could `if(this.checked)` – Arun P Johny Aug 11 '15 at 08:48
  • 1
    `if ($('#TwoFChk').change(function () {...}));` What are you trying to check here??? It would be always truthly. Anyway, your question is missing relevant code and context, you have to provide minimalistic sample replicating issue – A. Wolff Aug 11 '15 at 08:49
  • @A.Wolff Im trying to detect whenever the value of the checkbox has changed. And if that is the case, I need to check if its true and then change another checkboxes value. And not really, all I need to do is provide the JQuery code so people understand what im trying to convey – Andrew Kilburn Aug 11 '15 at 08:53
  • @AndrewKilburn And what if you remove the wrapping `if`, is the change event fired? Even it should be with the `if` but doesn't make sense. Anyway, again: `you have to provide minimalistic sample replicating issue` – A. Wolff Aug 11 '15 at 08:55
  • So I would need to post this to JSFiddle? Not really as it's not a particularly difficult question – Andrew Kilburn Aug 11 '15 at 08:57
  • @AndrewKilburn I miised that you are setting it `if ($(this).prop('checked', true))`, not checking it `if(this.checked)`, `$(this).prop('checked', true)` is always truthly value – A. Wolff Aug 11 '15 at 08:58
  • @A.Wolff Okay, thank you for your reply. I understand now – Andrew Kilburn Aug 11 '15 at 09:02

2 Answers2

2

Try this..

use "$(this).prop("checked") == true"

  $('#TwoFChk').change(function () {
                if($(this).prop("checked") == true){
                alert("It's checked");
                $('#ConfirmEmailChk').prop('checked', true);
            }
            });
Deenadhayalan Manoharan
  • 5,436
  • 14
  • 30
  • 50
0

If you want to see if a checkbox is checked with jQuery you can do so by using

$('#TwoFChk').is(':checked');
Gian Marco Toso
  • 11,676
  • 5
  • 29
  • 38