2

I have a problem with unchecking checkbox with dynamic id.

Both of them are checked but I need to set if one is clicked that other one is disabled then this is example:

<input id="value_Ture_do_30_15_0" class="rnr-checkbox" name="value_Ture_do_30_15[]" value="1" checked="checked" type="checkbox">

<input id="value_Ture_preko_30_15_0" class="rnr-checkbox" name="value_Ture_preko_30_15[]" value="1" checked="checked" type="checkbox">

I have tried this like:

$("[id^='value_Ture_do_30']").checked = false

Also this:

$(function () {
    $("[id^='value_Ture_do_30']).click(function (event) {
        if (event.target.checked) {
            $("[id^='value_Ture_preko_30']").find('input').removeAttr('checked');
        }
    });
    $("[id^='value_Ture_preko_30']").click(function (event) {
        if (event.target.checked) {
            $("[id^='value_Ture_do_30']").find('input').removeAttr('checked');
        }
    });
});

Also this solution did not work for me: Setting "checked" for a checkbox with jQuery? But it does not work. If you can help me Thanks

Solved it like this:

$("[id^='value_Ture']").click(function() {
    var $this = $(this), 
        wasChecked = $this.attr("checked") === "checked";
    $("[id^='value_Ture']:checked").removeAttr("checked");
    if (wasChecked) {
        $this.attr("checked", "checked");
    }
});
Community
  • 1
  • 1
Marin
  • 45
  • 1
  • 7

1 Answers1

1

You can use jQuery prop()

$("[id^='value_Ture_do_30']").prop('checked', false);

SUGGESTION

Why not using radios?

kapantzak
  • 11,610
  • 4
  • 39
  • 61