whoami
whoami

Reputation: 165

JQuery - Checkbox not returning correct value for "checked"

For the checkbox, i've put a function that will add on the checked property dynamically as below

if ($(_this).prop("checked") == true) {
    $(_this).attr('checked', 'checked');
}
else if ($(_this).prop("checked") == false) {
    $(_this).removeAttr('checked');
}

when it shows the below as the property for the checkbox

<center><input onclick=\"CheckboxIsChecked(this)\" type=\"checkbox\" checked=\"checked\"></center>

but when a function catch whether there is the checked property in the element as below. it would always return a false. What is wrong with my code?

$(List[i].Checkbox).prop("checked") ? true : false

Upvotes: 0

Views: 406

Answers (1)

Milan Chheda
Milan Chheda

Reputation: 8249

You can use is(":checked") to determine whether checkbox is checked or not:

function CheckboxIsChecked(elem) {
  console.log($(elem).is(":checked"));
  return $(elem).is(":checked");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<center><input onclick="CheckboxIsChecked(this)" type="checkbox"></center>

Upvotes: 1

Related Questions