Skip to content
Fix Code Error

How do I check whether a checkbox is checked in jQuery?

March 13, 2021 by Code Error
Posted By: Anonymous

I need to check the checked property of a checkbox and perform an action based on the checked property using jQuery.

For example, if the age checkbox is checked, then I need to show a textbox to enter age, else hide the textbox.

But the following code returns false by default:

if ($('#isAgeSelected').attr('checked')) {
  $("#txtAge").show();
} else {
  $("#txtAge").hide();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" id="isAgeSelected"/>
<div id="txtAge" style="display:none">
  Age is selected
</div>

How do I successfully query the checked property?

Solution

This worked for me:

$get("isAgeSelected ").checked == true

Where isAgeSelected is the id of the control.

Also, @karim79’s answer works fine. I am not sure what I missed at the time I tested it.

Note, this is answer uses Microsoft Ajax, not jQuery

Answered By: Anonymous

Related Articles

  • Is CSS Turing complete?
  • Active tab issue on page load HTML
  • What is the worst programming language you ever worked with?
  • insert tables in dataframe with years from 2000 to 20018…
  • Javascript validate all checkboxes are selected
  • Pandas pivot_table: filter on aggregate function
  • Polymer 1.x: Observers
  • Why is Ember throwing "Uncaught Error: Assertion Failed:…
  • d3.js - draw arrow line from border to border
  • Customize Bootstrap checkboxes

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

For-each over an array in JavaScript

Next Post:

How to change permissions for a folder and its subfolders/files in one step?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Get code errors & solutions at akashmittal.com
© 2022 Fix Code Error