Home >Web Front-end >JS Tutorial >How to Check Checkbox Status in jQuery?

How to Check Checkbox Status in jQuery?

Linda Hamilton
Linda HamiltonOriginal
2024-12-23 19:57:11660browse

How to Check Checkbox Status in jQuery?

How to Check Checkbox Status Using jQuery

In jQuery, the attr('checked') function is not used to check the checked property of a checkbox. Instead, you can use the following methods:

1. Using DOM Element's checked Property

The checked property of the checkbox DOM element provides the checked state of the checkbox:

if (document.getElementById('isAgeSelected').checked) {
  $("#txtAge").show();
} else {
  $("#txtAge").hide();
}

2. Using jQuery's change() Event

When the checkbox is clicked, the change() event is triggered. You can use it to toggle the visibility of the text box:

$('#isAgeSelected').click(function () {
    $("#txtAge").toggle(this.checked);
});

The above is the detailed content of How to Check Checkbox Status in jQuery?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn