• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
JavaScriptSource

JavaScriptSource

Search 5,000+ Free JavaScript Snippets

  • Home
  • Browse Snippets
    • Addon
    • Ajax
    • Buttons
    • Cookies
    • CSS
    • Featured
    • Forms
    • Games
    • Generators
    • Image Effects
    • Math Related
    • Miscellaneous
    • Multimedia
    • Navigation
    • Page Details
    • Passwords
    • Text Effects
    • Time & Date
    • User Details
Home / Forms / How To Check if a Checkbox is Checked With JavaScript or jQuery

How To Check if a Checkbox is Checked With JavaScript or jQuery

How To Check if a Checkbox is Checked With JavaScript or jQuery

Checking if a checkbox is checked in JavaScript can be done using the checked property of the checkbox element. This property returns a boolean value indicating whether the checkbox is currently checked or not.

Here’s an example of how to check if a checkbox with the ID “myCheckbox” is checked using JavaScript:

var checkbox = document.getElementById("myCheckbox");
if (checkbox.checked) {
  console.log("Checkbox is checked");
} else {
  console.log("Checkbox is not checked");
}

You can also use the .checked property to set the checkbox’s checked state:

var checkbox = document.getElementById("myCheckbox");
checkbox.checked = true; // Check the checkbox

jQuery also provides a way to check if a checkbox is checked. Here’s an example of how to check if a checkbox with the ID “myCheckbox” is checked using jQuery:

if ($("#myCheckbox").is(":checked")) {
  console.log("Checkbox is checked");
} else {
  console.log("Checkbox is not checked");
}

You can also use the .prop() method to set the checkbox’s checked state:

$("#myCheckbox").prop("checked", true); // Check the checkbox

Additionally, you can use the .attr() method to check and set the checkbox’s checked state:

// Check the checkbox
$("#myCheckbox").attr("checked", true);
// Check if checkbox is checked
if ($("#myCheckbox").attr("checked")) {
  console.log("Checkbox is checked");
} else {
  console.log("Checkbox is not checked");
}

Forms

Related Snippets:

  • Validate Numeric Only
  • Count the number of characters of a textarea
  • Virtual Keyboard Interface
  • How To Validate an Email Address In JavaScript

Primary Sidebar

FREE UPDATES!

Get the latest updates in your inbox for FREE!

Popular Posts

Story Generator

IP Grabber – get a users IP address with JavaScript

Simple Calendar

Remove Ads

Astrological Calculator

Copyright © 2023 JavaScriptSource.com

  • About
  • Privacy Policy
  • Submit
  • FAQ
  • Jobs For Developers
  • Contact Us